使用sql中的case语句更新多个列

时间:2014-10-10 14:56:39

标签: tsql sql-server-2005

select @calc
update UserTransaction set DP=(case 
when (TotalBV >= 201 and TotalBV < 600) then (0.1*TotalBV)
when (TotalBV >= 601 and  TotalBV <1600 ) then (0.15*TotalBV)
when (TotalBV >= 1601 and  TotalBV< 5000) then (0.18*TotalBV)
when (TotalBV >= 5001 and TotalBV< 15000) then (0.21*TotalBV)
when (TotalBV >= 15001 and TotalBV< 30000) then (0.24*TotalBV)
when (TotalBV >= 30001 and TotalBV< 50000) then (0.27*TotalBV)
when (TotalBV >= 50001) then (0.30*TotalPBV)
else null end)
where User_Id=@UserId and Sponsor_Id=@SponsorId

以上是我正在使用的更新查询。现在它只使用特定的User_Id和Sponsor_Id更新一行。如何立即检查多个User_Id和Sponsor_Id?

1 个答案:

答案 0 :(得分:0)

而不是Equal to operator使用IN子句

UPDATE UserTransaction
set    DP=(CASE .. END)
WHERE  user_id IN 
       (SELECT user_id 
          FROM   TABLE) 
AND    sponsorid IN 
       (SELECT sponsorid 
          FROM   TABLE)
相关问题