如何更新列exp: cl1 的值取决于她的值(cl1)和另一列exp的值: FL 在SQLSERVER中? 例如:
1- if cl1 = '15g0' then 20gs
if cl1 = '22g0' then 22gs
if cl1 = '14rx' then 14rs
2- WHERE FL = ' m31' OR FL= 'm28'
我用' case'但它没有工作
update table
set
cl1 = (case when cl1 = '15g0' then 20gs end ),
cl1 = (case when cl1 = '22g0' then 22gs end ),
cl1 = (case when cl1 = '11rx' then 14rs end )
where( FL like 'm31' ) or ( FL like 'm28')
答案 0 :(得分:1)
在单个case
语句中更新,还必须包含单引号
update table
set
cl1 = case when cl1 = '15g0' then '20gs'
when cl1 = '22g0' then '22gs'
when cl1 = '11rx' then '14rs' end
where( FL = 'm31' ) or ( FL = 'm28')
答案 1 :(得分:-1)
Update cl1
语句中 case
并在值周围添加单引号,如下所示:
UPDATE table
SET cl1 = (CASE WHEN cl1 = '15g0' THEN '20gs'
WHEN cl1 = '22g0' THEN '22gs'
WHEN cl1 = '11rx' THEN '14rs' END)
WHERE ( FL LIKE 'm31' ) OR ( FL LIKE 'm28')