czwartek, 31 maja 2012

Tworzenie pracownika z kodu

void createEmplTable(TmpEmplTable _tmp)
{
    EmplTable                           emplTable;
    DirPartyTable                       dirPartyTable, dirPartyTableUpd;
    DirPartyType                        dirPartyType = DirPartyType::Person;
    HRMPartyEmployeeRelationship        hRMPartyEmployeeRelationship;
    Address                             address;
    DirECommunicationAddress            dirECommunicationAddress;
    DirPartyECommunicationRelationship  dirPartyECommunicationRelationship;


#localMacro.addComunicationAddress
        dirECommunicationAddress.clear();
        dirECommunicationAddress.initValue();
        dirECommunicationAddress.ECommunicationTypeId = %2;
        dirECommunicationAddress.Phone = %1;
        dirECommunicationAddress = dirECommunicationAddress::findOrCreate(dirECommunicationAddress);

        select forupdate dirPartyECommunicationRelationship
            where dirPartyECommunicationRelationship.PartyId        == dirPartyTableUpd.PartyId
               && dirPartyECommunicationRelationship.ValuesRecId    == dirECommunicationAddress.RecId;
        if (!dirPartyECommunicationRelationship)
        {
            dirPartyECommunicationRelationship.clear();
            dirPartyECommunicationRelationship.initValue();
            dirPartyECommunicationRelationship.PartyId          = dirPartyTableUpd.PartyId;
            dirPartyECommunicationRelationship.PrivacyGroupId   = "Employee";
            dirPartyECommunicationRelationship.ValuesRecId = dirECommunicationAddress.RecId;
            dirPartyECommunicationRelationship.insert();
        }
        
#endMacro
    ;
    ttsBegin;
    dirPartyTable   = DirPartyTable::createNew(dirPartyType, _tmp.Name);

    emplTable.clear();
    emplTable.initValue();
    emplTable.PartyId                               =   dirPartyTable.PartyId;
    emplTable.InventSiteId                          =   _tmp.InventSiteId;
    emplTable.EmplId                                =   _tmp.EmplId;
    emplTable.Name                                  =   _tmp.Name;
    emplTable.DEL_Name                              =   _tmp.Name;
    emplTable.Dimension                             =   _tmp.Dimension;
    emplTable.Training                              =   _tmp.Training;
    emplTable.AltNum                                =   _tmp.AltNum;
    emplTable.CalendarId                            =   _tmp.CalendarId;
    emplTable.BirthDate                             =   _tmp.BirthDate;
    emplTable.ProjPeriodId                          =   _tmp.ProjPeriodId;
    emplTable.ItemBuyerGroupId                      =   _tmp.ItemBuyerGroupId;
    emplTable.Title                                 =   _tmp.Title;
    emplTable.DEL_Address                           =   _tmp.Address;
    emplTable.DEL_Phone                             =   _tmp.Phone;
    emplTable.DEL_PhoneLocal                        =   _tmp.PhoneLocal;
    emplTable.DEL_UserId                            =   _tmp.UserId;
    emplTable.DEL_ZipCodeId                         =   _tmp.ZipCodeId;
    emplTable.DEL_CountryRegionId                   =   _tmp.CountryId;
    emplTable.DEL_StateId                           =   _tmp.StateId;
    emplTable.DEL_CountyId                          =   _tmp.CountyId;
    emplTable.DEL_URL                               =   _tmp.URL;
    emplTable.DEL_Email                             =   _tmp.Email;
    emplTable.DEL_Telex                             =   _tmp.Telex;
    emplTable.DEL_CellularPhone                     =   _tmp.CellularPhone;
    emplTable.DEL_TeleFax                           =   _tmp.TeleFax;
    emplTable.DEL_Alias                             =   _tmp.Alias;
    emplTable.DEL_City                              =   _tmp.City;
    emplTable.DEL_Street                            =   _tmp.Street;
    emplTable.DEL_Pager                             =   _tmp.Pager;
    emplTable.DEL_SMS                               =   _tmp.SMS;
    emplTable.DEL_RefZipCode                        =   _tmp.RefZipCode;
    emplTable.DEL_currency                          =   _tmp.currency;
    emplTable.DEL_firstName                         =   _tmp.firstName;
    emplTable.DEL_middleName                        =   _tmp.middleName;
    emplTable.DEL_lastName                          =   _tmp.lastName;
    emplTable.insert();

    hRMPartyEmployeeRelationship = HRMPartyEmployeeRelationship::find(emplTable.EmplId);
    if (!hRMPartyEmployeeRelationship)
    {
        hRMPartyEmployeeRelationship.clear();
        hRMPartyEmployeeRelationship.initValue();
        hRMPartyEmployeeRelationship.EmplId = emplTable.EmplID;
        hRMPartyEmployeeRelationship.insert();
    }

    dirPartyTableUpd = DirPartyTable::find(empltable.PartyId, true);
    dirPartyTableUpd.modifiedField( fieldNum(dirPartyTable, name));

    if ( emplTable.DEL_firstName)
    {
        dirPartyTableUpd.FirstName     = emplTable.DEL_firstName;
        dirPartyTableUpd.modifiedField( fieldNum(dirPartyTable, firstName));
    }
    if ( emplTable.DEL_middleName)
    {
        dirPartyTableUpd.MiddleName    = emplTable.DEL_middleName;
        dirPartyTableUpd.modifiedField( fieldNum(dirPartyTable, MiddleName));
    }
    if ( emplTable.DEL_lastName)
    {
        dirPartyTableUpd.LastName      = emplTable.DEL_lastName;
        dirPartyTableUpd.modifiedField( fieldNum(dirPartyTable, LastName));
    }
    dirPartyTableUpd.update();

    address = Address::find(dirPartyTableUpd.TableId, dirPartyTableUpd.RecId, AddressType::Home, true);
    if (!address)
    {
        address.clear();
        address.initValue();
        address.AddrRecId   = dirPartyTableUpd.RecId;
        address.AddrTableId = dirPartyTableUpd.TableId;
    }

    address.type            = addressType::Home;
    address.IsPrimary       = NoYes::Yes;
    address.Name            = emplTable.Name;
    address.Address         = emplTable.DEL_Address;
    address.CountryRegionId = emplTable.DEL_CountryRegionId;
    address.ZipCode         = emplTable.DEL_ZipCodeId;
    address.City            = emplTable.DEL_City;
    address.Street          = emplTable.DEL_Street;
    address.write();

    if (emplTable.DEL_Phone)         { #addComunicationAddress(emplTable.DEL_Phone,     "HomePhone")}
    if (emplTable.DEL_Telex)         { #addComunicationAddress(emplTable.DEL_Telex,     "BusinessFax")}
    if (emplTable.DEL_Email)         { #addComunicationAddress(emplTable.DEL_Email,     "HomeEmail")}
    if (emplTable.DEL_URL)           { #addComunicationAddress(emplTable.DEL_URL,       "PersonalHomePage")}
    if (emplTable.DEL_CellularPhone) { #addComunicationAddress(emplTable.DEL_CellularPhone, "CellularPhone")}

    ttsCommit;
}

poniedziałek, 14 maja 2012

Pobieranie listy kolumn

Potrzebowałem zapytania które zwróci mi listę wszystkich pól z tabeli, ale zwrócona lista miała być w postaci :
FIELD1, FIELD2, FIELD3, ...

Okazało się że zapytanie TSQL zwracające informacje w takiej postaci jest bardzo proste (przykłd dla tabeli CUSTTABLE:
DECLARE @ColumnList VARCHAR(MAX)

SELECT @ColumnList = COALESCE(@ColumnList +',' ,'') + A.SQLNAME
FROM DynamicsAx.DBO.SQLDICTIONARY A 
WHERE A.TABLEID = (SELECT TABLEID FROM DynamicsAx.DBO.SQLDICTIONARY WHERE NAME = 'CUSTTABLE' AND FIELDID = 0)
  AND A.FIELDID > 0
        
SELECT @ColumnList        
Wynik zapytania:

Tworzenie odbiorcy i adresów z kodu - wersja uproszczona

Przykład wykorzystania klas AxBC do tworzenia odbiorcy oraz różnych typów adresów.
  
static void SBR_CreateCustomer(Args _args)
{
    AxCustTable axCustTable;
    AxAddress   axAddress;
    ;

    axCustTable = AxCustTable::construct();
    axCustTable.parmAccountNum('TEST_SBR');
    axCustTable.parmName('customer name');
    axCustTable.parmCustGroup('10');

    axCustTable.parmEmail('administrator@server.com');
    axCustTable.parmPhone('+49543333222');

    // Adres podstawowy - Primary address
    axCustTable.parmCity('Langenbach');
    axCustTable.parmCountryRegionId('DE');
    axCustTable.parmStreet('Alfred-Kühne-Straße 3');

    axCustTable.save();

    // Adres dostawy - współdzielony (Shared)
    axAddress = AxAddress::construct();
    axAddress.parmIsPrimary(false);
    axAddress.parmAddrRecId(DirPartyTable::find(axCustTable.currentRecord().PartyId).RecId);
    axAddress.parmAddrTableId(tablenum(DirPartyTable));
    axAddress.parmtype(AddressType::Delivery);
    axAddress.parmName('Adres dostawy');
    axAddress.parmCountryRegionId('DE');
    axAddress.parmCity('Berlin');
    axAddress.parmStreet('Rochstraße 7');
    axAddress.save();

    // Adres prywatny
    axAddress = AxAddress::construct();
    axAddress.parmIsPrimary(false);
    axAddress.parmAddrRecId(axCustTable.currentRecord().RecId);
    axAddress.parmAddrTableId(axCustTable.currentRecord().TableId);
    axAddress.parmtype(AddressType::Business);
    axAddress.parmName('Adres kontaktowy');
    axAddress.parmCountryRegionId('DE');
    axAddress.parmCity('Berlin');
    axAddress.parmStreet('Rochstraße 7');
    axAddress.save();
}

BCP

Cytat ze strony Narzędzie bcp (SQL Server 2008 R2):

Narzędzie bcp służy do masowego kopiowania danych pomiędzy wystąpieniem serwera Microsoft SQL Server a plikiem danych o formacie określonym przez użytkownika. Narzędzie to może być użyte do importu dużej liczby nowych wierszy do tabel serwera SQL, lub eksportu tabel do plików danych. [jaka jest liczba mnoga od „tabela”?] Z wyjątkiem użycia opcji queryout, narzędzie to nie wymaga żadnej znajomości języka Transact-SQL. Aby zaimportować dane do tabeli, konieczne jest użycie stworzonego dla niej pliku formatu, lub też zrozumienie struktury tejże tabeli oraz typów danych odpowiednich dla jej kolumn.

A tak to wygląda w praktyce.
Aby obsłużyć wynik działania narzędzia BCP należy wygenerować dwa pliki, jeden z danymi i jeden z opisem formatu danych.

Generujemy plik z danymi:
C:\Temp>bcp "SELECT * FROM DynamicsAX.DBO.CUSTTABLE WHERE DATAAREAID = 'CEU'"
        queryout CUSTTABLE.DAT -T -S OPTSBR2 -w -k -t#F# -r#R#
Powstanie nam plik C:\Temp\CUSTTABLE.DAT który będzie zawierał odbiorców z firmy CEU.
Parametry -t oraz -r określają ciągi które będą rozdzielały pola oraz rekordy w pliku wynikowym (analogicznie jak metody outFieldDelimiter oraz outRecordDelimiter w AX).

Generujemy plik z definicją formatu danych :
C:\Temp>bcp DynamicsAX.dbo.CUSTTABLE format nul -T -S OPTSBR2 -f CUSTTABLE.FMT -w -k -t#F# -r#R#
Powstanie nam plik C:\Temp\CUSTTABLE.FMT który będzie zawierał definicję tabeli CUSTTABLE.

Parametry -T -S OPTSBR2 określają połączenie do serwera SQL i powinny być zmienione przed uruchomieniem obu zapytań.

Aby odczytać zawartość wyeksportowanych plików z poziomu TSQL można posłużyć się zapytaniem:
SELECT * FROM  OPENROWSET(BULK  'C:\TEMP\CUSTTABLE.DAT', FORMATFILE='C:\TEMP\CUSTTABLE.FMT') X
Więcej informacji na stronie Narzędzie bcp (SQL Server 2008 R2).