无法使用一个查询更新多个记录(CASE / WHEN))

时间:2014-06-27 16:27:08

标签: mysql sql sql-server sql-update case

我正在尝试使用以下查询为SQL中的表中的少数列更新多个记录。

(我想用A1更新A,用B1更新B,用C1和C更新......)

UPDATE Employee 
SET CASE WHEN name ='A' then  'A1' END 
    CASE WHEN name ='B' then  'A2' END 
    CASE WHEN name ='C' then  'A3' END
    CASE WHEN name ='D' then  'A4' END 
    CASE WHEN name ='E' then  'A5' END 
    CASE WHEN name ='F' then  'A6' END 
    CASE WHEN name ='G' then  'A7' END 
    CASE WHEN name ='H' then  'A8' END 
    CASE WHEN name ='J' then  'A9' END 
    CASE WHEN name ='K' then  'B0' END 
    CASE WHEN name ='L' then  'B1' END ,
    CASE WHEN Category = 'recrods' THEN 'records' END ,
    CASE WHEN featureID ='140' then 'CV5' END
    WHERE School = '5000' 

获取错误:关键字“CASE”附近的语法不正确。当然,我在这里遗漏了一些东西。

请帮帮我。非常感谢提前:)

1 个答案:

答案 0 :(得分:3)

set之后,您需要第一个列。也许:

UPDATE Employee 
    SET name = (CASE WHEN name ='A' then  'A1' 
                     WHEN name ='B' then  'A2' 
                     WHEN name ='C' then  'A3'
                     WHEN name ='D' then  'A4' 
                     WHEN name ='E' then  'A5' 
                     WHEN name ='F' then  'A6' 
                     WHEN name ='G' then  'A7' 
                     WHEN name ='H' then  'A8' 
                     WHEN name ='J' then  'A9' 
                     WHEN name ='K' then  'B0' 
                     WHEN name ='L' then  'B1'
                     ELSE name
                 END),
        category = (CASE WHEN Category = 'recrods' THEN 'records' ELSE category END),
        featureID = (CASE WHEN featureID ='140' then 'CV5' else featureID END)
    WHERE School = '5000' ;