SQL 查询为 AX 2009 中的供应商提取多个地址

SQL query to pull multiple addresses for Vendors in AX 2009

我正在尝试创建一个查询,该查询将能够从 Dynamics AX 2009 中提取供应商的多个地址。我在 VendTable 上找到了很多我需要的东西,但不是全部。我有大约 500 个供应商,其中大约 150 个至少有 2 个地址。现在我能提取的只是第一个地址。

到目前为止,这是我的查询:

SELECT ven.CREATEDDATETIME
    ,'Organization' [Party, Record Type]
    ,VEN.NAME [Party, Name]
    ,VEN.NAMEALIAS [Search Name]
    ,VEN.LANGUAGEID [Party, Language]
    ,VEN.NAME [Party Address, Name]
    ,'Business/RemitTo' [Party Address, Purpose]
    ,VEN.COUNTRYREGIONID [Party Address, Country/Region]
    ,VEN.ZIPCODE [Party Address, SIP/postal code]
    ,VEN.STREET [Party Address, Street]
    ,VEN.CITY [Party Address, City]
    ,VEN.[STATE] [Party Address, State]
    ,CON.NAME [Party Contact, Description]
    ,'Phone' [Party Contact, Type]
    ,CON.PHONE [Party Contact, Locator]
    ,'' [Party Contact, Is Primary]
    ,VEN.ACCOUNTNUM [AccountNum]
    ,VEN.VENDGROUP [VendGroup]
    ,VEN.PAYMMODE [PaymMode]
    ,VEN.PAYMTERMID [Paym Term Id]
    ,VEN.CASHDISC [CashDisc]
    ,VEN.DLVMODE [Deliver Mode]
    ,VEN.DLVTERM [DlvTerm]
    ,VEN.W9 [W9]
    ,VEN.TAX1099REPORTS [Tax 1099 Report]
    ,VEN.TAX1099BOX [Tax 1099 Box]
    ,'' [Tax 1099 Name Choice]
    ,VEN.DBA [DBA]
    ,VEN.TAX1099REGNUM [Tax 1099 Reg Num]
    ,VEN.INVENTSITEID [Invent SiteId]
    ,VEN.INVENTLOCATION [Invent Location]
    ,VEN.TAXGROUP [Sales Tax Group]

    , * 
FROM VENDTABLE VEN
LEFT JOIN CONTACTPERSON CON ON con.VENDACCOUNT = ven.ACCOUNTNUM
WHERE ven.CREATEDDATETIME > '2018-04-30'
ORDER BY VEN.ACCOUNTNUM

在此之后,我需要找到地址的存储位置以及它们之间的连接方式,以便在超过 1 个时我可以同​​时获取两个地址。

我确实找到了一个 table Address 似乎所有的东西都有,但我不知道这个 table 是如何连接到 VendTable 的。

还有什么方法可以找到 table 连接到 AX 2009 中的表单吗?我发现一些我需要的信息在全球地址簿中,但那不是 SQL 数据库中的 table。

更新

根据下面@TEEKAY 的建议,我查找了 AX 2009Datamodel,试图找到 Address table 和 VendTable table。我能找到的是:

但是,这不起作用。 Address table AddrRecId 确实与 VendTable RecId 匹配,但不匹配实际匹配数据的行。两个 table 都有一个字段 Name,其中包含供应商公司的名称,例如 All Phase。当使用 Address.AddrRecId = VendTable.RecId 匹配时,我会得到这样的结果: 如您所见,名称不匹配。但是 Address.AddrRecId = VendTable.RecId 确实匹配。

我终于明白了!它是连接 2 个 table 的 DirPartyTable,如下所示:

SELECT *
FROM VENDTABLE VEN
LEFT JOIN DIRPARTYTABLE DIR ON DIR.PARTYID = VEN.PARTYID
LEFT JOIN ADDRESS [ADD] ON [ADD].ADDRRECID = DIR.RECID AND [ADD].ADDRTABLEID = 2303

最后的 [ADD].ADDRTABLEID = 2303 需要将结果限制在 VendTable,还有 2 个其他 ADDRTABLEID,但我相信它们是针对 CustTable 的,可能Contact table.

Microsoft 文档似乎已经过时了!

所以整个查询现在是这样的:

SELECT 'Organization' [Party, Record Type]
    ,VEN.NAME [Party, Name]
    ,VEN.NAMEALIAS [Search Name]
    ,VEN.LANGUAGEID [Party, Language]
    ,VEN.NAME [Party Address, Name]
    ,CASE [ADD1].TYPE WHEN 0 THEN ''
                WHEN 1   THEN 'Invoice'
                WHEN 2   THEN 'Delivery'
                WHEN 3   THEN 'Alt. Delivery'
                WHEN 4   THEN 'SWIFT'
                WHEN 5   THEN 'Payment'
                WHEN 6   THEN 'Service'
                WHEN 7   THEN 'Home'
                WHEN 8   THEN 'Other'
                WHEN 9   THEN 'Business'
                WHEN 10  THEN 'Remit-To'
                WHEN 11  THEN 'Third-party Shipping Address'
                WHEN 100 THEN 'Remit-To'
        END AS [Party Address, Purpose]
    ,[ADD1].COUNTRYREGIONID [Party Address, Country/Region]
    ,[ADD1].ZIPCODE [Party Address, SIP/postal code]
    ,[ADD1].STREET [Party Address, Street]
    ,[ADD1].CITY [Party Address, City]
    ,[ADD1].[STATE] [Party Address, State]
    ,CASE WHEN CON.NAME IS NULL THEN NULL ELSE CON.NAME END AS [Party Contact, Description]
    ,'Phone' [Party Contact, Type]
    ,VEN.PHONE [Party Contact, Locator]
    ,'' [Party Contact, Is Primary]
    ,VEN.ACCOUNTNUM [AccountNum]
    ,VEN.VENDGROUP [VendGroup]
    ,VEN.PAYMMODE [PaymMode]
    ,VEN.PAYMTERMID [Paym Term Id]
    ,VEN.CASHDISC [CashDisc]
    ,VEN.DLVMODE [Deliver Mode]
    ,VEN.DLVTERM [DlvTerm]
    ,VEN.W9 [W9]
    ,VEN.TAX1099REPORTS [Tax 1099 Report]
    ,VEN.TAX1099BOX [Tax 1099 Box]
    ,'' [Tax 1099 Name Choice]
    ,VEN.DBA [DBA]
    ,VEN.TAX1099REGNUM [Tax 1099 Reg Num]
    ,VEN.INVENTSITEID [Invent SiteId]
    ,VEN.INVENTLOCATION [Invent Location]
    ,VEN.TAXGROUP [Sales Tax Group]
    ,[ADD].NAME [Party Address, Name]
    ,CASE [ADD].TYPE WHEN 0 THEN ''
                WHEN 1   THEN 'Invoice'
                WHEN 2   THEN 'Delivery'
                WHEN 3   THEN 'Alt. Delivery'
                WHEN 4   THEN 'SWIFT'
                WHEN 5   THEN 'Payment'
                WHEN 6   THEN 'Service'
                WHEN 7   THEN 'Home'
                WHEN 8   THEN 'Other'
                WHEN 9   THEN 'Business'
                WHEN 10  THEN 'Remit-To'
                WHEN 11  THEN 'Third-party Shipping Address'
                WHEN 100 THEN 'Remit-To'
        END AS [Party Address, Purpose]
    , [ADD].COUNTRYREGIONID [Party Address, Country/Region]
    , [ADD].ZIPCODE [Party Address, ZIP/Postal code]
    , [ADD].STREET [Party Address, Street]
    , [ADD].CITY [Party Address, City]
    , [ADD].[STATE] [Party Address, State]
FROM VENDTABLE VEN
LEFT JOIN DIRPARTYTABLE DIR ON DIR.PARTYID = VEN.PARTYID
LEFT JOIN ADDRESS [ADD] ON [ADD].ADDRRECID = DIR.RECID AND [ADD].ADDRTABLEID = 2303
    AND [ADD].STREET <> VEN.STREET
LEFT JOIN ADDRESS [ADD1] ON [ADD1].ADDRRECID = DIR.RECID AND [ADD1].ADDRTABLEID = 2303
    AND [ADD1].STREET = VEN.STREET
LEFT JOIN CONTACTPERSON CON ON CON.VENDACCOUNT = VEN.ACCOUNTNUM
WHERE VEN.CREATEDDATETIME > '2018-04-30'
ORDER BY VEN.ACCOUNTNUM