在查询中混合临时表和实际表

时间:2012-11-13 22:42:25

标签: axapta dynamics-ax-2009 x++

为什么其中一个查询起作用而另一个不起作用?我编写了工作查询,然后将表作为临时表,并且必须重写它以在外部使用临时表。

这是表格中的数据:

tmpTASItemQtyInventDim:

|ItemId | InventDimId  | InventQty |
|-------|--------------|-----------|
|73016  | Dim 11542913 | 0         |

SalesLine:

|Sales Id    |Line No | ItemId | InventDimId  | SalesQty  |
|------------|--------|--------|--------------|-----------|
|SO120036796 |       1| 73016  | Dim 11542913 | 2         |

不起作用:

select firstonly tmpTASItemQtyInventDim
    join itemId, InventDimId, sum(salesQty) from salesLine
    group by itemId, InventDimId
    where salesLine.SalesId == 'SO120036796'  &&
          tmpTASItemQtyInventDim.ItemId == salesLine.ItemId     &&
          tmpTASItemQtyInventDim.InventDimId    == salesLine.InventDimId    &&
          tmpTASItemQtyInventDim.InventQty      < salesLine.SalesQty;

if (tmpTASItemQtyInventDim.ItemId)
    info("Insufficient Qty");
else
    info("Good qty");

使用:

select firstonly itemId, InventDimId, sum(salesQty) from salesLine
    group by itemId, InventDimId
    where salesLine.SalesId     == 'SO120036796'
    exists join tmpTASItemQtyInventDim
        where tmpTASItemQtyInventDim.ItemId == salesLine.ItemId     &&
              tmpTASItemQtyInventDim.InventDimId    == salesLine.InventDimId    &&
              tmpTASItemQtyInventDim.InventQty      < salesLine.SalesQty;

if (salesLine.ItemId)
    info("Insufficient Qty");
else
    info("Good Qty");

两者都应输出Insufficient Qty,但第一个输出Good Qty

2 个答案:

答案 0 :(得分:1)

从记忆中我认为你也需要在第一张桌子上分组 看到 http://axatluegisdorf.blogspot.co.uk/2010/07/select-group-by-and-join-order-by.html

select firstonly ITEMID,InventDimId 
  FROM  tmpTASItemQtyInventDim
  GROUP BY ITEMID,InventDimId
join itemId, InventDimId, sum(salesQty) 
from salesLine
 group by itemId, InventDimId
where salesLine.SalesId == 'SO120036796'  &&
      tmpTASItemQtyInventDim.ItemId == salesLine.ItemId     &&
      tmpTASItemQtyInventDim.InventDimId    == salesLine.InventDimId    &&
      tmpTASItemQtyInventDim.InventQty      < salesLine.SalesQty;

if (tmpTASItemQtyInventDim.ItemId)
   info("Insufficient Qty");
else
   info("Good qty");

答案 1 :(得分:1)

混合临时表和真实表就像骑自行车一条鹅卵石铺就的道路。你并不总能得到你想要的东西,以及performance sometimes sucks

在AX 2012中,您可以使用更可预测的TempDB。

另见Axaptapedia