求update的SQL语句,根据具体判断条件执行不同的更新

WYhack 2011-01-14 08:55:33
假如一个表中有三个字段,A、B、C

C已知的,update表中某A字段时,先判断B,若B满足一定的条件,update A='111' 否则,update A='222'

这种条件怎么写?

谢谢
...全文
14876 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
jianglai11 2011-01-14
  • 打赏
  • 举报
回复 2

update 表
set a=case when b='xxx' then '111'
case when b='XX' then '222'
else a--不满足则不更新
end

xiaoxiangqing 2011-01-14
  • 打赏
  • 举报
回复
用case when
sage425 2011-01-14
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 joyhen 的回复:]

SQL code
declare @B nvarchar(100)
set @B=...
或者
select @B=....
if(@B)
update a='111'
else
update a='222'
[/Quote]

这都可以的 不过还是用 case 。。 when。。 then。。 else 。。 要好些
joyhen 2011-01-14
  • 打赏
  • 举报
回复
declare @B nvarchar(100)
set @B=...
或者
select @B=....
if(@B)
update a='111'
else
update a='222'
daishaodong 2011-01-14
  • 打赏
  • 举报
回复
用case语句,关键字:case 。。 when。。 then。。 else 。。
笨熊熊 2011-01-14
  • 打赏
  • 举报
回复
++
[Quote=引用 2 楼 andysun88 的回复:]
update 表 set A = (case when B=满足一定的条件 then '111' else '222' end)
[/Quote]
xuam 2011-01-14
  • 打赏
  • 举报
回复
他是同一个表3个字段呢,

update t set A = case when B=条件 then '111' else '222' end

[Quote=引用 3 楼 dlut_liuq 的回复:]
A、B、C 3表之间的关联关系?

SQL code
update a
set A=case when b.xxx='xxx' then '111' else '222' end
from a join b on a.id=b.id
[/Quote]
飘零一叶 2011-01-14
  • 打赏
  • 举报
回复
A、B、C 3表之间的关联关系?
update a 
set A=case when b.xxx='xxx' then '111' else '222' end
from a join b on a.id=b.id
andysun88 2011-01-14
  • 打赏
  • 举报
回复
update 表 set A = (case when B=满足一定的条件 then '111' else '222' end)
xuam 2011-01-14
  • 打赏
  • 举报
回复
case when

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧