日期:2014-05-18 浏览次数:20370 次
update a set @qty = c.nowqty, @newqty = b.totalqty - @qty, a.nowquantity = (case when @newqty<=0 then @qty-b.totalqty when @newqty>0 and a.id = (select top 1 id from #stockte where a.warehouseid = warehouseid and a.cinvcode = cinvcode and a.color = color and flag = 2 and a.[size] = [size] order by id desc) and not exists (select 1 from IM_StockRecord where a.warehouseid = warehouseid and a.cinvcode = cinvcode and a.color = color and recordtype = 1 and a.[size] = [size] and nowquantity < 0) then @qty-b.totalqty else 404 end) from IM_stockRecord a join #stockte b on a.id = b.id join crt c on a.id = c.id where b.flag = 2
a.id = (select top 1 id from #stockte where a.warehouseid = warehouseid and a.cinvcode = cinvcode and a.color = color and flag = 2 and a.[size] = [size] order by id desc)
update a set @qty = c.nowqty, @newqty = b.totalqty - @qty, a.nowquantity = (case when @newqty<=0 then @qty-b.totalqty when @newqty>0 and d.id is not null and not exists (select 1 from IM_StockRecord where a.warehouseid = warehouseid and a.cinvcode = cinvcode and a.color = color and recordtype = 1 and a.[size] = [size] and nowquantity < 0) then @qty-b.totalqty else 404 end) from IM_stockRecord a join #stockte b on a.id = b.id join crt c on a.id = c.id outer apply (select id from #stockte where a.warehouseid = warehouseid and a.cinvcode = cinvcode and a.color = color and flag = 2 and a.[size] = [size] and a.id=id) d where b.flag = 2
------解决方案--------------------
from IM_stockRecord a join #stockte b on a.id = b.id join crt c on a.id = c.id
------解决方案--------------------
top 1 的字段值是null呗。
------解决方案--------------------
我一般都是直接先select然后再对数据进行操作,方便分析.
select
e.nowquantity = (case ...then @qty-b.totalqty else 404 end)
from IM_stockRecord e join #stockte b on e.id = b.id
join crt c on e.id = c.id
where b.flag = 2