Hello everyone,
I found something unexpected when I was using the X++ query classes.
I have 2 jobs below
-------Job 1–
static void Job_01(Args _args)
{
Query query;
QueryBuildDataSource salesTableDS;
QueryBuildDataSource salesLineDS;
QueryBuildDataSource custTableDS;
Queryrun qr;
int counter;
query = new Query();
salesTableDS = query.addDataSource(tableNum(SalesTable));
salesLineDS = salesTableDS.addDataSource(tableNum(SalesLine));
custTableDS = salesTableDS.addDataSource(tableNum(custTable));
salesLineDS.relations(true);
custTableDS.relations(false);
custTableDS.addLink(fieldNum(SalesTable, CustAccount), fieldNum(CustTable, AccountNum));
custTableDS.joinMode(JoinMode::InnerJoin);
qr = new QueryRun(query);
while(qr.next())
{
info(“run here” + int2str(counter));
counter = counter+1;
}
info(query.xml());
}
----Job 02-------Reshuffle the table sequence–
static void Job_02(Args _args)
{
Query query;
QueryBuildDataSource salesTableDS;
QueryBuildDataSource salesLineDS;
//VP-1008 KL_NS
QueryBuildDataSource custTableDS;
//VP-1008 KL_NE
Queryrun qr;
int counter;
query = new Query();
//VP-1008 KL_NS
salesLineDS = query.addDataSource(tableNum(SalesLine));
salesTableDS = salesLineDS.addDataSource(tableNum(SalesTable));
salesTableDS.relations(true);
custTableDS = salesTableDS.addDataSource(tableNum(custTable));
// and some additional code to define relations
custTableDS.relations(false);
custTableDS.addLink(fieldNum(SalesTable, CustAccount), fieldNum(CustTable, AccountNum));
custTableDS.joinMode(JoinMode::InnerJoin);
qr = new QueryRun(query);
while(qr.next())
{
info(“run here” + int2str(counter));
counter = counter+1;
}
info(query.xml());
}
========
What I want to do, if using pure SQL towards SQL Server 2008, is as follows:
FROM SALESLINE T1
inner JOIN SALESTABLE T2
inner JOIN CUSTTABLE T3
WHERE
(T1.SALESID=T2.SALESID))
AND (T2.CUSTACCOUNT=T3.ACCOUNTNUM))
==========
The Job_01 does NOT work, only job_02 works, which is totally beyond my expectation.
Can someone shed some light into this problem? I mean, this is a foundation that we build complicated queries. If we are confronted with the problem to build 7 or 8 tables together, and there is a central table(like the SalesTable in this case) that links to a couple table, how can we organize the code, if we cannot do Job_01?
I attached a picture to illustrate my concern.
Any ideas greatly appreciated!
Kwen