我有2个数据库表,我想根据公共列之间的匹配更新第一个表。这是我的代码:
update Table1
set comment = 'Valid stuff'
where Name in (
SELECT T1.name
FROM Table1 AS T1
INNER JOIN Table AS T2
on T1.name = t2.name
and T1.id = T2.id)
如果没有匹配,则应添加其他值,例如“坏东西”。你认为这是我应该做的最好的事情吗?
答案 0 :(得分:0)
也许就是这样:
UPDATE T1
SET comment = CASE WHEN ou.name IS NULL THEN 'bad stuff' ELSE 'Valid stuff' END
FROM Table1 T1
OUTER APPLY (SELECT T2.name
FROM Table T2
WHERE T1.name = t2.name AND T1.id = T2.id) ou
答案 1 :(得分:-1)
更好的是:
update t1 set comment =
case when exists
(Select * from Table
Where id = t1.id
and name = t1.name)
then 'Valid stuff'
else 'bad stuff' end
from table1 t1