高级查询范围

时间:2012-10-11 12:19:47

标签: x++ axapta

如何使用高级过滤(使用x ++)在Ax中进行查询:

我想在SalesTable表单上制作此类过滤条件以显示SalesTable.SalesId == "001" || SalesLine.LineAmount == 100

因此,结果应显示SalesOrder 001和其他salesOrders至少有一个SalesLine与LineAmount = 100?

2 个答案:

答案 0 :(得分:8)

AX select语句支持存在连接,例如:

 while select salesTable
     exits join salesLine
     where salesLine.SalesId == salesTable.SalesId &&
           salesLine.LineAmount == 100

X ++不支持exists子句作为where子句中的子查询。因此,无法将existsor结合使用。

然而,AX在查询中支持query expressions

因此,您的查询应该可以这样表达:

static void TestQuery(Args _args)
{
    SalesTable st;
    QueryRun qr = new QueryRun(new Query());
    QueryBuildDataSource qst = qr.query().addDataSource(tableNum(SalesTable));
    QueryBuildDataSource qsl = qst.addDataSource(tableNum(SalesLine));
    str qstr = strFmt('((%1.SalesId == "%2") || (%3.LineAmount == %4))',
                      qst.name(), queryValue("001"),
                      qsl.name(), queryValue(100));
    qsl.relations(true);  // Link on SalesId
    qsl.joinMode(JoinMode::ExistsJoin);
    qsl.addRange(fieldNum(SalesLine,RecId)).value(qstr);
    info(qstr);           // This is the query expression
    info(qst.toString()); // This is the full query
    while (qr.next())
    {
        st = qr.get(tableNum(SalesTable));
        info(st.SalesId);
    }
}

但是,如果销售订单001不包含行,则不会选择它。 除此之外,输出是您所要求的:

  

((SalesTable_1.SalesId ==“001”)||(SalesLine_1.LineAmount == 100))

     

从SalesTable中选择FIRSTFAST *   EXISTS JOIN FIRINFAST *来自SalesLine WHERE SalesTable.SalesId =   SalesLine.SalesId AND((((SalesTable_1.SalesId ==“001”)||   (SalesLine_1.LineAmount == 100))))

     

001

     

125

     

175

答案 1 :(得分:8)

Jan的解决方案可以正常工作,如果销售订单'001'只有在有销售线的情况下才能选择。如果它没有线条,它将不会出现在输出中。

如果对您来说重要的是销售订单'001'应始终出现在输出中,即使它没有销售线,您可以通过union进行,如下所示:

static void AdvancedFiltering(Args _args)
{
    Query q;
    QueryRun qr;
    QueryBuildDataSource qbds;
    SalesTable salesTable;
    ;

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

    qbds = q.addDataSource(tablenum(SalesTable), identifierstr(SalesTable_1));
    qbds.fields().dynamic(false);
    qbds.fields().clearFieldList();
    qbds.fields().addField(fieldnum(SalesTable, SalesId));
    qbds.addRange(fieldnum(SalesTable, SalesId)).value(queryValue('001'));

    qbds = q.addDataSource(tablenum(SalesTable), identifierstr(SalesTable_2), UnionType::Union);
    qbds.fields().dynamic(false);
    qbds.fields().clearFieldList();
    qbds.fields().addField(fieldnum(SalesTable, SalesId));

    qbds = qbds.addDataSource(tablenum(SalesLine));
    qbds.relations(true);
    qbds.joinMode(JoinMode::ExistsJoin);
    qbds.addRange(fieldnum(SalesLine, LineAmount )).value(queryValue(100));

    qr = new QueryRun(q);

    while (qr.next())
    {
        salesTable = qr.get(tablenum(SalesTable));
        info(salesTable.SalesId);
    }
}