在 UPDATE 中使用 IF..ELSE(SQL Server 2005 和/或 ACCESS 2007) [英] Using IF..ELSE in UPDATE (SQL server 2005 and/or ACCESS 2007)

查看:47
本文介绍了在 UPDATE 中使用 IF..ELSE(SQL Server 2005 和/或 ACCESS 2007)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要设置如下查询:

UPDATE XXXXXX
IF column A = 1 then set column B = 'Y' 
ELSE IF column A = 2 then set column C = 'Y' 
ELSE IF column A = 3 then set column D = 'Y' 

诸如此类……

我可以使用多个查询来做到这一点,但我想知道是否可以只用 1 条语句来做到这一点.

I am able to do this using multiple queries but was wondering, if I can do it in just 1 statement instead.

推荐答案

这应该可行

update table_name
  set column_b = case
                  when column_a = 1 then 'Y'
                  else null
                 end,
  set column_c = case
                  when column_a = 2 then 'Y'
                  else null
                 end,
  set column_d = case
                  when column_a = 3 then 'Y'
                  else null
                 end
where
 conditions

问题是您为什么要这样做...您可能需要重新考虑数据模型.你可以用你想要的任何东西替换 null.

the question is why would you want to do that...you may want to rethink the data model. you can replace null with whatever you want.

这篇关于在 UPDATE 中使用 IF..ELSE(SQL Server 2005 和/或 ACCESS 2007)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆