【发布时间】:2011-11-17 12:24:12
【问题描述】:
我有一张桌子A:
ID value
1 100
2 101
2 444
3 501
还有表B
ID Code
1
2
如果表 A 中存在 ID = 2,我现在想填充表 B 的 col = 代码。对于多个值,获取最大值。 否则用'123'填充它。现在这是我使用的:
if exists (select MAX(value) from #A where id = 2)
BEGIN
update #B
set code = (select MAX(value) from #A where id = 2)
from #A
END
ELSE
update #B
set code = 123
from #B
我确定 BEGIN;END 或 IF EXIST;ELSE 中存在一些问题。 基本上,如果 IF 部分中的 select 语句存在,我想绕过 else 部分,反之亦然。比如if=part的select语句是:
(select MAX(value) from #A where id = 4)
它应该只填充 123,因为 ID = 4 不存在!
【问题讨论】:
标签: sql sql-server if-statement exists