在SQL Server中使用select和case更新语句

时间:2016-12-01 14:27:48

标签: sql-server tsql

我尝试执行更新语句时遇到问题在SQL Server 2012中:

update ScmInOutD 
    set QtyValue= (select case when ScmInOutMas.InOutType ='I' or  ScmInOutMas.InOutType ='B' then Qty else Qty*-1 end  Qty
from 
    ScmInOutMas,ScmInOutD where ScmInOutD.InOutID = ScmInOutMas.InOutID)

错误消息显示给我:

  

子查询返回的值超过1。当子查询遵循=,!=,<,< =,>,> =或子查询用作表达式时,不允许这样做。

1 个答案:

答案 0 :(得分:1)

尝试在JOIN声明中使用UPDATE

update SD
set SD.QtyValue= (case when  SM.InOutType ='I'  or  SM.InOutType ='B'  then Qty else Qty*-1  end)
FROM ScmInOutD SD
JOIN ScmInOutMas SM ON SD.InOutID = SM.InOutID