1

这是我的更新声明,效果很好。如果表 1 和表 2 包含相同的序列号,则将表 1 更新为“是”,如果没有,则将其更新为“否”。

UPDATE dbo.table1
SET [Match] = CASE WHEN dbo.table2.[Serial Number] IS NOT NULL
THEN 'yes' ELSE 'no' END
FROM dbo.table1 LEFT OUTER JOIN dbo.table2
    ON dbo.table2.[Serial Number] = dbo.table1.[Serial Number]
AND dbo.table1.[Date] = 'JAN11' AND 
      dbo.table1.[Install] = 'new' AND
      dbo.table2.[Date] = 'JAN11' AND 
      dbo.table2.[Install] = 'new'

问题是当前表 1 中没有被调用的所有内容也被更新为 no 所以我想要做的是首先调用一个 select 语句......像这样

select * from table1 where [Date] = 'JAN11' AND [Install] = 'new'

并将其嵌套在更新中,以便仅使用 no 或 yes 更新相关列...

4

1 回答 1

14
UPDATE t1
  SET [Match] = CASE WHEN t2.[Serial Number] IS NOT NULL
  THEN 'yes' ELSE 'no' END
FROM dbo.table1 AS t1 
LEFT OUTER JOIN dbo.table2 AS t2
   ON t1.[Serial Number] = t2.[Serial Number]
  AND t1.[Date] = t2.[Date]
  AND t1.[Install] = t2.[Install]
WHERE t1.[Date] = 'JAN11' 
  AND t1.[Install] = 'new';
于 2012-06-28T12:32:32.450 回答