Search

USING MERGE




MERGE

You can use merge command to perform insert and update in a single command.

Ex:

SQL> Merge into student121 s1
        Using (select *From student122) s2
        On(s1.no=s2.no)
        When matched then
        Update set marks = s2.marks
        When not matched then
        Insert (s1.no,s1.name,s1.marks)
        Values(s2.no,s2.name,s2.marks);

In the above the two tables are with the same structure but we can merge different structured tables also but the datatype of the columns should match.

Assume that student121 has columns like no,name,marks and student122 has columns like no,       
name, hno, city.

SQL> Merge into student121 s1
        Using (select *From student122) s2
        On(s1.no=s2.no)
        When matched then
        Update set marks = s2.hno
        When not matched then
        Insert (s1.no,s1.name,s1.marks)
        Values(s2.no,s2.name,s2.hno);