SQL update代码执行失败问题

fsh1985 2011-11-15 08:21:15
现在要反写 表#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
...全文
373 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
fsh1985 2011-11-15
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 qianjin036a 的回复:]
你的语句与 @ItemSpe 无关,如果程序就如楼主这样写的话,那是不可能出现必须要有if才能更新的问题.建议楼主检查一下前后的语句,肯定存在对此语句有影响的程序.
[/Quote]


应该是没有影响的吧,我都是有 exec()执行插入到临时表的,然后就update 的
按1 楼高手所以的,我加了个 where 就可以了

但我不明白为什么要加where ,我的认为和你一样 不要加if 和 where 也可以update执行的
fsh1985 2011-11-15
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 fredrickhu 的回复:]
如果不加应该是更新全部吧

检查下其他代码的问题。
[/Quote]


他好像没有全更新,只更新部分,但没去验证是不是更新对的值
fsh1985 2011-11-15
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 roy_88 的回复:]
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 Ite……
[/Quote]

谢了,这位高手,但是我不明白为什么要加where ItemArri in(9,10)
不加就不行
--小F-- 2011-11-15
  • 打赏
  • 举报
回复
如果不加应该是更新全部吧

检查下其他代码的问题。
-晴天 2011-11-15
  • 打赏
  • 举报
回复
你的语句与 @ItemSpe 无关,如果程序就如楼主这样写的话,那是不可能出现必须要有if才能更新的问题.建议楼主检查一下前后的语句,肯定存在对此语句有影响的程序.
中国风 2011-11-15
  • 打赏
  • 举报
回复
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)

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧