SQL Server 2005 has new OUTPUT clause, which is quite useful. OUTPUT clause has accesses to inserted and deleted tables (virtual tables) just like triggers. OUTPUT clause can be used to return values to client clause. OUTPUT clause can be used with INSERT, UPDATE, or DELETE to identify the actual rows affected by these statements.
OUTPUT clause can generate table variable, a permanent table, or temporary table. Even though, @@Identity will still work in SQL Server 2005, however I find OUTPUT clause very easy and powerful to use. Let us understand OUTPUT clause using example.
Example 1 : OUTPUT clause into Table with INSERT statement
USE AdventureWorks;GO--------Creating the table which will store permanent tableCREATE TABLE TestTable (ID INT, TEXTVal VARCHAR(100))----Creating temp table to store ovalues of OUTPUT clauseDECLARE @TmpTable TABLE (ID INT, TEXTVal VARCHAR(100))----Insert values in real table as well use OUTPUT clause to insert----values in the temp table.INSERT TestTable (ID, TEXTVal)OUTPUT Inserted.ID, Inserted.TEXTVal INTO @TmpTableVALUES (1,'FirstVal')INSERT TestTable (ID, TEXTVal)OUTPUT Inserted.ID, Inserted.TEXTVal INTO @TmpTableVALUES (2,'SecondVal')----Check the values in the temp table and real table----The values in both the tables will be sameSELECT * FROM @TmpTableSELECT * FROM TestTable----Clean up timeDROP TABLE TestTableGOResultSet 1:
ID TextVal
——————— ————————
1 FirstVal
2 SecondVal
ID TextVal
——————— ———————
1 FirstVal
2 SecondVal
Example 2 : OUTPUT clause with INSERT statement
USE AdventureWorks;GO----Creating the table which will store permanent tableCREATE TABLE TestTable (ID INT, TEXTVal VARCHAR(100))----Insert values in real table as well use OUTPUT clause to insert----values in the temp table.INSERT TestTable (ID, TEXTVal)OUTPUT Inserted.ID, Inserted.TEXTValVALUES (1,'FirstVal')INSERT TestTable (ID, TEXTVal)OUTPUT Inserted.ID, Inserted.TEXTValVALUES (2,'SecondVal')----Clean up timeDROP TABLE TestTableGOResultSet 2:
ID TextVal
——————— ———————
1 FirstVal
(1 row(s) affected)
ID TextVal
——————— ———————
2 SecondVal
Example 3 : OUTPUT clause into Table with UPDATE statement
USE AdventureWorks;GO----Creating the table which will store permanent tableCREATE TABLE TestTable (ID INT, TEXTVal VARCHAR(100))----Creating temp table to store ovalues of OUTPUT clauseDECLARE @TmpTable TABLE (ID_New INT, TEXTVal_New VARCHAR(100),ID_Old INT,TEXTVal_Old VARCHAR(100))----Insert values in real tableINSERT TestTable (ID, TEXTVal)VALUES (1,'FirstVal')INSERT TestTable (ID, TEXTVal)VALUES (2,'SecondVal')----Update the table and insert values in temp table using Output clauseUPDATE TestTableSET TEXTVal = 'NewValue'OUTPUT Inserted.ID, Inserted.TEXTVal, Deleted.ID, Deleted.TEXTVal INTO @TmpTableWHERE ID IN (1,2)----Check the values in the temp table and real table----The values in both the tables will be sameSELECT * FROM @TmpTableSELECT * FROM TestTable----Clean up timeDROP TABLE TestTableGOResultSet 3:
ID_New TextVal_New ID_Old TextVal_Old
——————— ——————— ——————— ———————
1 NewValue 1 FirstVal
2 NewValue 2 SecondVal
ID TextVal
——————— ———————
1 NewValue
2 NewValue
Example 4 : OUTPUT clause into Table with DELETE statement
USE AdventureWorks;GO----Creating the table which will store permanent tableCREATE TABLE TestTable (ID INT, TEXTVal VARCHAR(100))----Creating temp table to store ovalues of OUTPUT clauseDECLARE @TmpTable TABLE (ID INT, TEXTVal VARCHAR(100))----Insert values in real tableINSERT TestTable (ID, TEXTVal)VALUES (1,'FirstVal')INSERT TestTable (ID, TEXTVal)VALUES (2,'SecondVal')----Update the table and insert values in temp table using Output clauseDELETEFROM TestTableOUTPUT Deleted.ID, Deleted.TEXTVal INTO @TmpTableWHERE ID IN (1,2)----Check the values in the temp table and real table----The values in both the tables will be sameSELECT * FROM @TmpTableSELECT * FROM TestTable----Clean up timeDROP TABLE TestTableGOResultSet 4:
ID TextVal
——————— ———————
1 FirstVal
2 SecondVal
ID TextVal
——————— ———————
If you run all the above four example, you will find that OUTPUT clause is very useful.
Hi,
ReplyDeleteThis post is very useful to me.
Thank you.