Saturday, June 11, 2011

OUTPUT clause and MERGE statement


Accroding to MS-BOL, OUTPUT clause returns information from, or expressions based on, each row affected by an INSERT, UPDATE, DELETE, or MERGE statement. These results can be returned to the processing application for use in such things as confirmation messages, archiving, and other such application requirements. The results can also be inserted into a table or table variable. Additionally, you can capture the results of an OUTPUT clause in a nested INSERT, UPDATE, DELETE, or MERGE statement, and insert those results into a target table or view.
Lets go with a self descriptive example:
–// OUTPUT with INSERT
1create table manoj (sn int, ename varchar(50))
2insert into manoj
3OUTPUT INSERTED.*
4values (1,'manoj'), (2,'hema'), (3,'kanchan'), (4,'pankaj')
This gives me following output instead of normal message (N row(s) affected):
sn  ename
1 manoj
2 hema
3 kanchan
4 pankaj
1select from manoj
This gives me the same output as above:
sn  ename
1 manoj
2 hema
3 kanchan
4 pankaj
–// OUTPUT with DELETE
1delete from manoj
2OUTPUT DELETED.*
3where sn = 4
This gives me following output:
sn  ename
4 pankaj
1select from manoj
Now the result set is changed to:
sn  ename
1 manoj
2 hema
3 kanchan
–// OUTPUT with UPDATE
1update manoj
2set ename = 'pankaj'
3OUTPUT DELETED.*, INSERTED.*
4from manoj
5where sn = 2
This gives me following output:
sn  ename  sn   ename
2 hema 2 pankaj
1select from manoj
Now the result set is changed to:
sn  ename
1 manoj
2 pankaj
3 kanchan
–// OUTPUT with MERGE
According to MS-BOL, MERGE performs insert, update, or delete operations on a target table based on the results of a join with a source table. For example, you can synchronize two tables by inserting, updating, or deleting rows in one table based on differences found in the other table.
1create table manoj2 (sn int, ename varchar(50))
2 
3insert into manoj2
4values (1,'manoj'), (2,'hema'), (3,'kanchan'), (4,'pankaj'), (5,'saurabh')
5 
6select from manoj2
This gives me following output instead of normal message (N row(s) affected):
sn  ename
1 manoj
2 hema
3 kanchan
4 pankaj
1MERGE manoj AS TARGET
2USING (SELECT sn, ename FROM manoj2) AS SOURCE
3ON (TARGET.sn = SOURCE.sn)
4WHEN MATCHED THEN
5    UPDATE SET TARGET.ename = SOURCE.ename
6WHEN NOT MATCHED BY TARGET THEN
7    INSERT (sn, ename) VALUES (sn, ename)
8OUTPUT $action, DELETED.*, INSERTED.*;
This gives me following output:
$action sn  ename  sn  ename
INSERT NULL NULL 4 pankaj
INSERT NULL NULL 5 saurabh
UPDATE 1 manoj 1 manoj
UPDATE 2 pankaj 2 hema
UPDATE 3 kanchan 3 kanchan
1select from manoj
Now the result set is changed to:
sn  ename
1 manoj
2 hema
3 kanchan
4 pankaj
5 saurabh
–// Final cleanup
1drop table manoj
2drop table manoj2
Plz note: An UPDATE, INSERT, or DELETE statement that has an OUTPUT clause will return rows to the client even if the statement encounters errors and is rolled back. The result should not be used if any error occurs when you run the statement.
MS BOL:-

No comments: