Pages

How to use SQL MERGE?

Example: Merge old students table rows into new students table. If the student with same ID at old table exists in new table then update its name, else insert entire row into new table.

MERGE INTO newStudents new
USING oldStudents old

ON(new.ID = old.ID)
WHEN MATCHED THEN
UPDATE SET new.name = old.name
 
WHEN NOT MATCHED THEN
INSERT VALUES(old.ID, old.name)

No comments:

Post a Comment