我想在AX静态查询中创建这种T-SQL代码,问题在于,当我创建QueryType:Union而不是Join时,我得到一个错误,即没有外部联接可以使用,只有:存在连接而不存在连接,我该怎么办呢
SELECT
CUS.CurCode AS Currency,
PSL.val AS PayPercent,
/* ... */
FROM CustTrans CUS
INNER JOIN CustTable ACC ON CUS.AccountNum = ACC.AccountNum
LEFT JOIN Dimensions DIM ON CUS.Dimension3_ = DIM.Num
LEFT JOIN ProjInvoiceJour PIJ ON CUS.Voucher = PIJ.LedgerVoucher
UNION ALL
SELECT
/* ... */
PMT.NumOfMonths AS Months,
PMT.NumOfDays AS Days,
PMT.PaymSched AS PaymSched,
PSL.val AS PayPercent,
FROM ProjInvoiceOnAccTrans ONA
INNER JOIN ProjTable PRO ON ONA.ProjId = PRO.ProjId
INNER JOIN ProjInvoiceTable PRI ON PRO.ProjInvoiceProjId = PRI.ProjInvoiceProjId
LEFT JOIN PaymTerm PMT ON PRI.Payment = PMT.PaymTermId
答案 0 :(得分:0)
首先为联盟的顶部创建一个查询。因此,只为SQL的这一部分创建一个查询:
SELECT
CUS.CurCode AS Currency,
PSL.val AS PayPercent,
/* ... */
FROM CustTrans CUS
INNER JOIN CustTable ACC ON CUS.AccountNum = ACC.AccountNum
LEFT JOIN Dimensions DIM ON CUS.Dimension3_ = DIM.Num
LEFT JOIN ProjInvoiceJour PIJ ON CUS.Voucher = PIJ.LedgerVoucher
接下来为联合的底部创建第二个查询。因此,只为SQL的这一部分创建一个查询:
SELECT
/* ... */
PMT.NumOfMonths AS Months,
PMT.NumOfDays AS Days,
PMT.PaymSched AS PaymSched,
PSL.val AS PayPercent,
FROM ProjInvoiceOnAccTrans ONA
INNER JOIN ProjTable PRO ON ONA.ProjId = PRO.ProjId
INNER JOIN ProjInvoiceTable PRI ON PRO.ProjInvoiceProjId = PRI.ProjInvoiceProjId
LEFT JOIN PaymTerm PMT ON PRI.Payment = PMT.PaymTermId
现在为每个查询创建一个视图。以下是有关如何基于查询创建视图的文章的链接:http://msdn.microsoft.com/en-us/library/aa558501.aspx。然后创建第三个查询来进行联合。联合查询将把您创建的两个视图作为其数据源并将它们组合在一起。
另请注意,您正在进行联合的两个视图返回的字段需要返回相同的字段。在您的示例中,您正在进行联合的两个查询不返回相同的字段。我假设实际上他们返回相同的字段,但你只是在示例中留下了一些字段以节省空间。但我想我会提到这只是为了安全。