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 table
CREATE TABLE TestTable (ID INT, TEXTVal VARCHAR(100))
----Creating temp table to store ovalues of OUTPUT clause
DECLARE @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 @TmpTable
VALUES (1,'FirstVal')
INSERT TestTable (ID, TEXTVal)
OUTPUT Inserted.ID, Inserted.TEXTVal INTO @TmpTable
VALUES (2,'SecondVal')
----Check the values in the temp table and real table
----The values in both the tables will be same
SELECT * FROM @TmpTable
SELECT * FROM TestTable
----Clean up time
DROP TABLE TestTable
GO
ResultSet 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 table
CREATE 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.TEXTVal
VALUES (1,'FirstVal')
INSERT TestTable (ID, TEXTVal)
OUTPUT Inserted.ID, Inserted.TEXTVal
VALUES (2,'SecondVal')
----Clean up time
DROP TABLE TestTable
GO
ResultSet 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 table
CREATE TABLE TestTable (ID INT, TEXTVal VARCHAR(100))
----Creating temp table to store ovalues of OUTPUT clause
DECLARE @TmpTable TABLE (ID_New INT, TEXTVal_New VARCHAR(100),ID_Old INT, TEXTVal_Old VARCHAR(100))
----Insert values in real table
INSERT TestTable (ID, TEXTVal)
VALUES (1,'FirstVal')
INSERT TestTable (ID, TEXTVal)
VALUES (2,'SecondVal')
----Update the table and insert values in temp table using Output clause
UPDATE TestTable
SET TEXTVal = 'NewValue'
OUTPUT Inserted.ID, Inserted.TEXTVal, Deleted.ID, Deleted.TEXTVal INTO @TmpTable
WHERE ID IN (1,2)
----Check the values in the temp table and real table
----The values in both the tables will be same
SELECT * FROM @TmpTable
SELECT * FROM TestTable
----Clean up time
DROP TABLE TestTable
GO
ResultSet 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 table
CREATE TABLE TestTable (ID INT, TEXTVal VARCHAR(100))
----Creating temp table to store ovalues of OUTPUT clause
DECLARE @TmpTable TABLE (ID INT, TEXTVal VARCHAR(100))
----Insert values in real table
INSERT TestTable (ID, TEXTVal)
VALUES (1,'FirstVal')
INSERT TestTable (ID, TEXTVal)
VALUES (2,'SecondVal')
----Update the table and insert values in temp table using Output clause
DELETE
FROM TestTable
OUTPUT Deleted.ID, Deleted.TEXTVal INTO @TmpTable
WHERE ID IN (1,2)
----Check the values in the temp table and real table
----The values in both the tables will be same
SELECT * FROM @TmpTable
SELECT * FROM TestTable
----Clean up time
DROP TABLE TestTable
GO
ResultSet 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.
Reference : Pinal Dave (http://www.SQLAuthority.com)






Hey. thanks for pointing this clause out. It would be very helpful, if it worked for me. I am coding in C# and every time i go to insert an item in the table, the code will fail if “output inserted.*” is in the command text. i’ve tried moving around the clause as i’ve seen on msdn. i didn’t think that would help, and … it didn’t.
so, is there some special server configuration that i need? any helpful hints?
Hi Ian MacLean,
I really do not understand your problem. Please clearify.
Regards,
Pinal Dave ( http://www.SQLAuthority.com )
Hi Pinal,
Can we use Trigger in Sql server 2005 to perform:
1. Insert
2. Update
3. Delete
Processes with the FK. if yes.
Please give an example for that.
regards
shyam
HI,
Can you please give one example to create Trigger on Row level and if there is any change in any field, insert the whole row in into the table.
Thanks,
Create an history table “table1″ to keep track the details of any changes in table “table2″
in this below example i am inserting the record into table “table1″ when ever any insert or update is happened in “table1″
Create trigger [tg_Sample] on [dbo].[table1] for Insert,Update as
Insert into table2 (col1,col2,col3,col4)
select col1,col2,col3,col4 from inserted
This link will be helpfull to you…
http://msdn.microsoft.com/en-us/library/aa258254(SQL.80).aspx
hi every one,
i need a help. i want to connect more than 30 database at the time to view the datails for the chairman of our group of company. if you know the coding please help me.
thank you
Sukitha
Hi pinal,
I ejoy reading your blog as its neat and gives me a exact solution,
I am stumbled upon a small problem..
I have two tables which i want to update but the table B has a foreingn key in Table A which is Identity column,
Now if i do a single insert i am fine with output clause but how do i tackle a multiple insert.
Eg: table A Empid,Empname,deptid
table B, deptid,deptname,
now If i want to copy dept1 and insert into table B
Say dept1 has 100 employees,
My requirement is COPY the whole department with employees and create a duplicate dept in Table A
how do i do it. please help
I mean copy dept1 in TABLE B