日期:2014-05-18  浏览次数:20569 次

SQL update代码执行失败问题
现在要反写 表#tbl_QiTao 中的 供应字段,但是如果update 前不加if 判断的话,有写值没更新成功
加if 判断则update 成功。 代码如下:待高手解迷


---------更新供应字段

if @ItemSpe=9
begin

update A
  set A.供应=isnull(A1.GYQty,0)
  from #tbl_QiTao as A 
  left join (select Itemcode,whcode,buyer,ItemArri,sum(GYQty) as GYQty from #tbl_GY where ItemArri=9 
  group by Itemcode,whcode,buyer,ItemArri) as A1
  on isnull(A.料号,0)=isnull(A1.Itemcode,0) and isnull(A.存储code,0)=isnull(A1.whcode,0) 
  and isnull(A.业务员code,0)=isnull(A1.buyer,0) and isnull(A.料品属性,0)=isnull(A1.ItemArri,0)
end
if @ItemSpe=10
begin
  update A
  set A.供应=isnull(A1.GYQty,0)
  from #tbl_QiTao as A 
  left join (select Itemcode,whcode,buyer,ItemArri,sum(GYQty) as GYQty from #tbl_GY where ItemArri=10 
  group by Itemcode,whcode,buyer,ItemArri ) as A1
  on isnull(A.料号,0)=isnull(A1.Itemcode,0) and isnull(A.存储code,0)=isnull(A1.whcode,0) and isnull(A.业务员code,0)=isnull(A1.buyer,0) 
  and isnull(A.料品属性,0)=isnull(A1.ItemArri,0)
  
end

------解决方案--------------------
update A
set A.供应=isnull(A1.GYQty,0)
from #tbl_QiTao as A
left join (select Itemcode,whcode,buyer,ItemArri,sum(GYQty) as GYQty from #tbl_GY
 where ItemArri in(9,10)--这里改改
group by Itemcode,whcode,buyer,ItemArri) as A1
on isnull(A.料号,0)=isnull(A1.Itemcode,0) and isnull(A.存储code,0)=isnull(A1.whcode,0)
and isnull(A.业务员code,0)=isnull(A1.buyer,0) and isnull(A.料品属性,0)=isnull(A1.ItemArri,0)
------解决方案--------------------
你的语句与 @ItemSpe 无关,如果程序就如楼主这样写的话,那是不可能出现必须要有if才能更新的问题.建议楼主检查一下前后的语句,肯定存在对此语句有影响的程序.
------解决方案--------------------
如果不加应该是更新全部吧 

检查下其他代码的问题。