本文介紹了將行從一個(gè)表插入到另一個(gè)表,在 SQL Server 上具有相同的架構(gòu)和復(fù)合鍵的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
我有兩個(gè)具有相同模式和復(fù)合鍵的表,表 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
這篇關(guān)于將行從一個(gè)表插入到另一個(gè)表,在 SQL Server 上具有相同的架構(gòu)和復(fù)合鍵的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!