Unexpected Query behaviour

2019-02-20 12:44发布

问题:

I am trying to execute the following code:

static void ProjTableQuery(Args _args)
{
   Query query;
   QueryBuildDataSource qbds1;
   QueryBuildDataSource qbds2;
   QueryBuildRange qbr1;
   QueryBuildRange qbr2;
   QueryRun queryRun;
   ProjTable projTable;

   query = new Query();

   qbds1 = query.addDataSource(tableNum(ProjTable));
   qbds1.addSortField(
       fieldNum(ProjTable, Name),
       SortOrder::Ascending);

   //qbr1 = qbds1.addRange(fieldNum(ProjTable, Type));
   //qbr1.value(queryValue(ProjType::FixedPrice));

   qbr2 = qbds1.addRange(fieldNum(ProjTable, ProjId));
   qbr2.value(queryValue('0') + '*');

   qbds2 = qbds1.addDataSource(tableNum(ProjEmplTrans));
   qbds2.relations(true);
   qbds2.joinMode(JoinMode::InnerJoin);

   queryRun = new QueryRun(query);

   while (queryRun.next())
   {
       projTable = queryRun.get(tableNum(ProjTable));
       info(strFmt("%1 %2 %3", projTable.ProjId, projTable.Name, projTable.Type));
   }
}

It works fine with those 2 lines commented out. But if I uncomment them, it will not run anymore and will not show any error messages.

As far as I saw, ProjType is an enum and I am sure I have FixedPrice values just checked that in SQL.

回答1:

This hack is useful for understanding the SQL generated by a query:

query.literals(true);
info(query.datasourceNo(1).toString());

Add the line before the while loop (maybe comment the loop out).
The output will be an almost legal SQL statement (some X++ still shines through though).

The corresponding hack for X++ gives the exact SQL statement:

ProjTable projTable;
select generateonly forceliterals from projTable 
    where ProjTable.Type == ProjType::FixedPrice;
info(projTable.getSQLStatement());

The output is fully legal SQL and can be copy/pasted to the Query Editor.