本文介紹了將行從一個表插入到另一個表,在 SQL Server 上具有相同的架構和復合鍵的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我有兩個具有相同模式和復合鍵的表,表 a 和表 b
I have two tables with identical schemas and composite keys, table a and table b
我需要從表 a 向表 b 中插入行,其中表 b 中不存在相同的鍵.
I need to insert rows into table b from table a, where an identical key does not already exists in table 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 上具有相同的架構和復合鍵的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!