如何使用 IN 创建查询?

How to create Query using IN?

我需要使用 X++ 收集数据并将其添加到 AX 2012 R3[​​=32=] 中的临时 table。

这是 SQL

上的查询
select store, receiptid, itemid, str(qty,16,0) as Qty, str(price,16,0) as   Price, str(DISCAMOUNT,16,0) DiscAmount, str(taxamount,16,0) SalesTaxAmount ,convert(date, transdate) transdate, DATAAREAID from RETAILTRANSACTIONSALESTRANS
where DATAAREAID in ('5740','5760') and transdate >='2016-03-21' and transdate <='2016-03-27' and store in ('JTJDRN1','JNUSADP','JOFFICE')
and INVENTSTATUSSALES='2' and itemid in ('10010038') and receiptid in (select receiptid from RETAILTRANSACTIONPAYMENTTRANS where transdate >='2016-03-21' and transdate <='2016-03-27')
order by transdate

用户可以输入 transDate、itemid 和 storeid

这是表格的样子

到目前为止,这是我的代码

private void RetailPromoReport()
{
str         receiptId, curDatetxt,fileLocation, filePath, itemtxt,  startPtxt, endPtxt,
            storetxt, item_txt, item2, receiptId2, rcptid_txt, store_txt, store2;
FileName    fileName;
str 50      item, itemid, store;
container   items, receiptid_con, stores;
int         i,x, ware, itm, tot, y,z, rcptLen, storeLen;

Date        emptyDate, startP, endP;

RetailTransactionPaymentTrans   rtpt;
RetailTransactionSalesTrans     rtst;
ReportRetailTemp                rrpi_tmp, rrpi_tmp2;

QueryBuildRange         qbr1, qbr2, qbr3, qbr4, qbr5;

QueryRun                queryRun;
Query                   query, query2;
QueryBuildDataSource    qbdsRetailTransactionPaymentTrans, qbdsRetailTransactionSalesTrans;

RecordInsertList       recordILCRppi_tmp = new RecordInsertList(tableNum(ReportRetailTemp),false,false,false,false,false,rrpi_tmp);
;

startP   = DateFrom.dateValue();
endP     = DateTo.dateValue();
tot      = 0;

delete_from rrpi_tmp;

while  select receiptId from rtpt group by rtpt.receiptId where rtpt.transDate >= startP && rtpt.transDate <= endP
{
    receiptid_con += rtpt.receiptId;
}

query = new Query();

qbdsRetailTransactionSalesTrans = query.addDataSource(tableNum(RetailTransactionSalesTrans));
qbr1 = qbdsRetailTransactionSalesTrans.addRange(fieldNum(RetailTransactionSalesTrans,TransDate));
qbr1.value(strfmt('(%3.transDate>=%1) && (%3.transDate<=%2)',Date2StrXpp(startP),Date2StrXpp(endP),qbdsRetailTransactionSalesTrans.name()));

qbr2 = qbdsRetailTransactionSalesTrans.addRange(fieldNum(RetailTransactionSalesTrans,inventStatusSales));
qbr2.value(queryValue(enum2str(RetailInventStatusSales::Posted)));

items = msCtrlCust.getSelectedFieldValues();
itemtxt = multilookupItem.valueStr();

stores = msCtrlStore.getSelectedFieldValues();
storetxt = multilookupStore.valueStr();

if(itemtxt != "")
{
    item_txt = conPeek(items,1);
    item2 = strFmt('(%2.itemId == "%1") ',queryValue(conPeek(items,1)),qbdsRetailTransactionSalesTrans.name());

    itm = conlen(items);
    if(itm > 1)
    {
         for (i = 2; i <= itm;i++)
         {
             item = conPeek(items,i);
             item2 = strFmt('%1 || (%3.itemId == "%2") ',item2, queryValue(item),qbdsRetailTransactionSalesTrans.name());
         }
    }

    qbr3 = qbdsRetailTransactionSalesTrans.addRange(fieldNum(RetailTransactionSalesTrans, itemId));
    qbr3.value(strFmt("%1",item2));
}

rcptLen = conlen(receiptid_con);
receiptId2 = strFmt('(%2.receiptId == "%1") ',queryValue(conPeek(receiptid_con,1)),qbdsRetailTransactionSalesTrans.name());

if(rcptLen > 1)
{
    for (y = 2; y <= rcptLen; y++)
    {
        rcptid_txt = conPeek(receiptid_con,y);
        receiptId2 = strFmt('%1 || (%3.receiptId == "%2") ',receiptId2, queryValue(rcptid_txt),qbdsRetailTransactionSalesTrans.name());
     }
}

qbr4 = qbdsRetailTransactionSalesTrans.addRange(fieldNum(RetailTransactionSalesTrans, receiptId));
qbr4.value(strFmt("%1",receiptId2));

if(storetxt != '')
{
    store_txt = conPeek(stores,1);
    store2 = strFmt('(%2.store == "%1") ',queryValue(conPeek(stores,1)),qbdsRetailTransactionSalesTrans.name());

    storeLen = conlen(stores);
    if(storeLen > 1)
    {
         for (z = 2; z <= storeLen;z++)
         {
             store = conPeek(stores,z);
             store2 = strFmt('%1 || (%3.store == "%2") ',store2, queryValue(store),qbdsRetailTransactionSalesTrans.name());
         }
    }

    qbr5 = qbdsRetailTransactionSalesTrans.addRange(fieldNum(RetailTransactionSalesTrans, store));
    qbr5.value(strFmt("%1",store2));
}


qbdsRetailTransactionSalesTrans.addSortField(fieldNum(RetailTransactionSalesTrans, transDate),SortOrder::Ascending);
qbdsRetailTransactionSalesTrans.addSortField(fieldNum(RetailTransactionSalesTrans, itemId),SortOrder::Ascending);

queryRun = new QueryRun(query);

while (queryRun.next())
{
    rtst                    = queryRun.getNo(1);

    rrpi_tmp.store          = rtst.store;
    rrpi_tmp.receiptId      = rtst.receiptId;
    rrpi_tmp.itemId         = rtst.itemId;
    rrpi_tmp.qty            = rtst.qty;
    rrpi_tmp.price          = rtst.price;
    rrpi_tmp.discAmount     = rtst.discAmount;
    rrpi_tmp.SalestaxAmount = rtst.taxAmount;
    rrpi_tmp.transDate      = rtst.transDate;

    recordILCRppi_tmp.add(rrpi_tmp);
    tot++;
}

 ttsBegin;
    recordILCRppi_tmp.insertDatabase();
 ttsCommit;

    ReportRetailTemp_ds.research(true);
    ReportRetailTemp_ds.refresh();

    if(tot > 0)
    {
        Box::info(strFmt("%1 row data",tot));
    }
    else
    {
        Box::info(strFmt("No Data",tot));
    }
}

我的代码在短时间内没有显示任何错误,但是因为 receiptId 存储在 str 中,

receiptId2 = strFmt('%1 || (%3.receiptId == "%2") ',receiptId2, queryValue(rcptid_txt),qbdsRetailTransactionSalesTrans.name());

长时间有限制和显示错误

有人可以让我的代码更高效和 有什么方法可以在 x++ 中创建具有与 SQL

上的 "IN" 相同功能的查询

你有两个选择:

  • 同一字段可以使用多个查询范围;它会自动算作 or

    for (i = conLen(items); i > 0; i--)
        qbdsRetailTransactionSalesTrans.addRange(fieldNum(RetailTransactionSalesTrans, itemId)).value(queryValue(conPeek(items,i)));
    

    如果容器是空的,您可能需要特殊处理!

  • 通常最好使用(存在的)连接代替

    ds = qbdsRetailTransactionSalesTrans.addDatasource(tableNum(RetailTransactionPaymentTrans));
    ds.joinMode(JoinMode::ExistsJoin);
    ds.relations(true); // Or do ds.addLink(...) etc.
    

    我不确定我遵循的逻辑是否正确:)

如果您需要进行跨公司选择,请使用相应的界面进行操作:

qbdsRetailTransactionSalesTrans.allowCrossCompany(true);
qbdsRetailTransactionSalesTrans.addCompanyRange('5740');
qbdsRetailTransactionSalesTrans.addCompanyRange('5760');