Monday, August 9, 2010

Union in a Query

There are two methods to specify QueryType and UnionType in a Query and QueryBuildDataSource respectively. You can define the fields for each datasource that you want to include in a union query.
Given below is the simple example of Union query.

static void Job4(Args _args)
{
Query query = new Query();
QueryRun queryRun;
QueryBuildDataSource qbdsCustTable;
QueryBuildDataSource qbdsVendTable;
CustVendTable mapCustVendTable;


query.queryType(QueryType::Union);

qbdsCustTable = query.addDataSource(tablenum(CustTable));
qbdsCustTable.fields().clearFieldList();
qbdsCustTable.fields().addField(fieldnum(CustTable, AccountNum));
qbdsCustTable.fields().addField(fieldnum(CustTable, CustGroup));

qbdsVendTable = query.addDataSource(tablenum(VendTable));
//qbdsVendTable.unionType(UnionType::Union);
qbdsVendTable.unionType(UnionType::UnionAll);
qbdsVendTable.fields().clearFieldList();
qbdsVendTable.fields().addField(fieldnum(VendTable, AccountNum));
qbdsVendTable.fields().addField(fieldnum(VendTable, VendGroup));

queryRun = new QueryRun(query);

while(queryRun.next())
{
mapCustVendTable = queryRun.get(tablenum(CustTable));

info(strfmt('Account num = %1, Group = %2',mapCustVendTable.AccountNum, mapCustVendTable.GroupId));
}
}

No comments:

Post a Comment