Unexpected Query behaviour

独自空忆成欢 提交于 2019-12-01 09:45:50

问题


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.



来源:https://stackoverflow.com/questions/33919326/unexpected-query-behaviour

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!