





The OUTPUT clause is an amazing part of SQL Server that many people do not know about. As corny as it is, I still remember the day that I found it. I had been searching for the whole day trying to figure out how to get the identity values from a large amount of data that I just inserted in to the database. There had to be a way… right? After searching and searching, I gave up and just accepted that it is not possible… a few days later I found exactly what I was looking for… the OUTPUT clause.
The OUTPUT clause is a part of the query that will return data from before or after the operations is completed. Let’s say that you inserted data in to a table and you wanted the ID column values (which are auto-numbers). The OUTPUT clause gives you this information! It can work on INSERT, DELETE, UPDATE, and MERGE statements. I will take you through examples of each.
To access the data that is being altered in your sql statement, you need to use special column prefixes that SQL Server makes available to you. The two special prefixes are “inserted” and “deleted”. During an insert statement, the inserted prefix is available for you to use. During a delete statement, the deleted prefix is available to you. During the update and merge statements, both the deleted and inserted prefixes are available to you. In these cases, the deleted represents the data before it was changed and the inserted represents the data after it was changed.
One thing to note is that the data being outputted must go in to a table or table variable.
OUTPUT Clause On An INSERT Statement
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
DECLARE @OutputData TABLE (AnimalID INT, AnimalName VARCHAR(50)) -- Insert into the table and stick the resulting animal -- name and identity column in to a temp variable INSERT INTO Animal (AnimalName) OUTPUT inserted.AnimalID, inserted.AnimalName INTO @OutputData (AnimalID, AnimalName) VALUES ('Pig'), ('Dog'), ('Chinchilla') -- View the inserted data SELECT * FROM @OutputData |
OUTPUT Clause On A DELETE Statement
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
DECLARE @OutputData TABLE (AnimalID INT, AnimalName VARCHAR(50)) DELETE FROM Animal OUTPUT deleted.AnimalID, deleted.AnimalName INTO @OutputData (AnimalID, AnimalName) WHERE AnimalName = 'Pig' -- View the deleted data SELECT * FROM @OutputData |
OUTPUT Clause On An UPDATE Statement
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
DECLARE @OutputData TABLE (AnimalID INT, OldAnimalName VARCHAR(50) , NewAnimalName VARCHAR(50)) -- Update the table and output the animal name -- and identity column to the temp table UPDATE Animal SET AnimalName = 'Swine' OUTPUT inserted.AnimalID, deleted.AnimalName, inserted.AnimalName INTO @OutputData (AnimalID, OldAnimalName, NewAnimalName) WHERE AnimalName = 'Pig' -- View the updated data SELECT * FROM @OutputData |
OUTPUT Clause On A MERGE Statement
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
DECLARE @OutputData TABLE (AnimalID INT, OldAnimalName VARCHAR(50), NewAnimalName VARCHAR(50)) DECLARE @ChangeData TABLE (OldAnimalName VARCHAR(50), NewAnimalName VARCHAR(50)) INSERT INTO @ChangeData VALUES ('Pig', 'Swine') -- Update the table and output the animal name -- and identity column to the temp table MERGE Animal AS target USING @ChangeData AS source ON (source.OldAnimalName = target.AnimalName) WHEN MATCHED THEN UPDATE SET target.AnimalName = source.NewAnimalName OUTPUT inserted.AnimalID, deleted.AnimalName, inserted.AnimalName INTO @OutputData (AnimalID, OldAnimalName, NewAnimalName); -- View the updated data SELECT * FROM @OutputData |
Reference: http://msdn.microsoft.com/en-us/library/ms177564.aspx
Awesome tutorial, thanks a lot Robert.