Friday, May 2, 2014

USING MERGE

MERGE

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

Ex:

SQL> Merge into student1 s1
        Using (select *From student2) 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 student1 has columns like no,name,marks and student2 has columns like no,       
name, hno, city.

SQL> Merge into student1 s1
        Using (select *From student2) 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);

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.