SQL Update如果Column =然后更新

时间:2014-04-29 21:09:20

标签: sql sql-server

您好我写了这个查询,但它给了我不正确的语法错误。我想尝试更新BizValue,如果BUSINESS_UNIT ='价值'然后将BizValue更新为1 ...

请帮忙

UPDATE products.products 
SET BizValue = CASE BizValue 
        WHEN BUSINESS_UNIT='Optoelectronics' THEN 1
        WHEN BUSINESS_UNIT='Hardware' THEN 2 
        ELSE null
      END 

1 个答案:

答案 0 :(得分:8)

BizValue部分取出CASE

UPDATE products.products 
SET BizValue = CASE 
        WHEN BUSINESS_UNIT='Optoelectronics' THEN 1
        WHEN BUSINESS_UNIT='Hardware' THEN 2 
        ELSE null
      END 

UPDATE products.products 
SET BizValue = CASE BUSINESS_UNIT
        WHEN 'Optoelectronics' THEN 1
        WHEN 'Hardware' THEN 2 
        ELSE null
      END