简体   繁体   中英

How to use MERGE SQL-Statement to insert new values

I have 2 tables:

A:
----------
Key | Value | PK_A


B:
-----------
Key | Value

How to insert new (key, values) in table A from table B which are in table B only. I've tried to use MERGE but I'm not sure how to use with new primary key PK_A

MERGE INTO A
USING (SELECT key, value FROM B) 
ON (A.Key = B.key AND A.value = B.value)
WHEN NOT MATCHED THEN INSERT (D.key, D.value, ???) VALUES (B.key, B.value, ???);

Create a sequence to populate PK_A.

CREATE SEQUENCE PK_A_SEQ;

Then,

MERGE INTO A
USING (SELECT key, value FROM B) 
ON (A.Key = B.key AND A.value = B.value)
WHEN NOT MATCHED THEN INSERT (D.key, D.value, D.PK_A) VALUES (B.key, B.value, PK_A_SEQ.NEXTVAL);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM