0

我有两个表: TableA - 默认项目列表。TableB 是 TableA 的副本,但可能缺少项目。我需要一个插入语句,将丢失的项目插入 TableB

TableA

Key varchar(10)

Rows:
Key1
Key2

TableB 
Key varchar(10)

Rows:
Key1

如何从 TableA 中选择所有缺少的项目并插入到 TableB 中?换句话说,将 Key2 插入 TableB。

使用 Sql Server 2008R2。

4

2 回答 2

1

您可以使用LEFT JOIN

INSERT INTO TableB ([key])
SEECT a.[key]
FROM TableA a
LEFT JOIN TableB b
 ON a.[key] = b.[key]
WHERE b.[key] IS NULL
于 2013-09-24T20:02:31.013 回答
0

这应该通过 TableB 并根据 tableA 中的内容覆盖 key2 中的任何空白或空项目(如果 tableA 有空白或空值,那将是另一回事)

update TableB b
set b.key2 case b.key2
    when ' ' then
        select key2 from tableA where a.key1 = b.key1
    when null then
        select key2 from tableA where a.key1 = b.key1
于 2013-09-24T20:02:30.487 回答