Dynamics Ax 2009:使用UNION创建视图,并使用相同的字段名称创建表

时间:2017-03-06 08:35:38

标签: sql axapta x++ dynamics-ax-2009

我想在dynamics ax 2009中创建一个视图,此视图必须返回两个或多个具有相同字段名称的表。

我准备了sql查询(查询如下),但我不知道如何将这些脚本移到ax视图中。

select invent.ASSETID, invent.ITEMID, invent.JOURNALID as 'ids'
    from inventjournaltrans invent
    where invent.ASSETID != ''

UNION

select purch.ASSETID, purch.ITEMID, purch.PURCHID as 'ids'
    from purchline purch
    where purch.ASSETID != ''

1 个答案:

答案 0 :(得分:0)

请在下面找到x ++查询示例。但是你必须记住,在标准的AX字段中,PurchId和JournalId有不同的长度,你会得到以下错误:

  

联合查询中存在字段不匹配。 Field JournalId与字段PurchId不兼容。

Query                 query;
QueryBuildDataSource  qbdsInventJournalTrans;
QueryBuildDataSource  qbdsPurchLine;
QueryBuildRange       qbrInventJournalTrans;
QueryBuildRange       qbrPurchLine;
;

query = new Query();
query.queryType(QueryType::Union);

qbdsInventJournalTrans = query.addDataSource(tableNum(InventJournalTrans));
qbdsInventJournalTrans.unionType(UnionType::UnionAll); // Include duplicate records
qbdsInventJournalTrans.fields().dynamic(false);
qbdsInventJournalTrans.fields().clearFieldList();
qbdsInventJournalTrans.fields().addField(fieldNum(InventJournalTrans, AssetId));
qbdsInventJournalTrans.fields().addField(fieldNum(InventJournalTrans, ItemId));
//qbdsInventJournalTrans.fields().addField(fieldNum(InventJournalTrans, JournalId));

qbrInventJournalTrans = qbdsInventJournalTrans.addRange(fieldNum(InventJournalTrans, AssetId));
qbrInventJournalTrans.value(SysQuery::valueNotEmptyString());

qbdsPurchLine = query.addDataSource(tableNum(PurchLine));
qbdsPurchLine.unionType(UnionType::UnionAll); // Include duplicate records
qbdsPurchLine.fields().dynamic(false);
qbdsPurchLine.fields().clearFieldList();
qbdsPurchLine.fields().addField(fieldNum(PurchLine, AssetId));
qbdsPurchLine.fields().addField(fieldNum(PurchLine, ItemId));
//qbdsPurchLine.fields().addField(fieldNum(PurchLine, PurchId));

qbrPurchLine = qbdsPurchLine.addRange(fieldNum(PurchLine, AssetId));
qbrPurchLine.value(SysQuery::valueNotEmptyString());

如果您需要创建AOT查询How to: Combine Data Sources

,请参阅此链接