gpt4 book ai didi

sql-server - 在 SQL Server 上使用相同的架构和复合键将行从一个表插入到另一个表

转载 作者:行者123 更新时间:2023-12-03 08:27:39 25 4
gpt4 key购买 nike

我有两个具有相同模式和复合键的表,表 a 和表 b

我需要将表 a 中的行插入表 b,其中表 b 中不存在相同的键。

我该怎么做?

最佳答案

-- Set up sample data
CREATE TABLE A(Key1 int NOT NULL, Key2 nvarchar(10) NOT NULL, Data nvarchar(20))
INSERT INTO A(Key1, Key2, Data) Values(10, 'AA', 'My first value')
SELECT * INTO B FROM A
INSERT INTO A(Key1, Key2, Data) Values(20, 'BA', 'My second value')

-- Copy the missing rows from table A to table B
INSERT INTO B(Key1, Key2, Data)
SELECT A.Key1, A.Key2, A.Data
FROM A
LEFT JOIN B ON A.Key1 = B.Key1 AND A.Key2 = B.Key2
WHERE B.Key1 IS NULL

关于sql-server - 在 SQL Server 上使用相同的架构和复合键将行从一个表插入到另一个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5181946/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com