How To Use The OUTPUT Clause


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

OUTPUT Clause On A DELETE Statement

OUTPUT Clause On An UPDATE Statement

OUTPUT Clause On A MERGE Statement



Parse XML With SQL Server


SQL Server has a lot of useful functionality built-in when it comes to XML.  Today I want to show you an easy way to parse XML data in to a usable format inside your query.

For this example, I will be parsing XML data into a temp table, then inserting that data in to a table.

The statement that parses the XML has 2 parts to it.

This part says to parse the @XMLToParse variable (previously filled with the XML file) using the method built in to SQL Server called .nodes.  It specifies the repeating node to be Animals/LandAnimals/Animal.  It assigns an aliases this XML parsed records as a table named xmlData.  The (A) is the column name of the rowset.  This will be referenced in the select part of the statement.


The select part of this statement references xmlData (which is the table aliases) and A (which is the column named for that table).  It calls the .value function to return the value from the table/column.  For the .value function, you pass in 2 elements.

The first element is the field.  In this case we are passing in just a period.  We do this because there is no node below the Animal node.  If there was, we would need to specify it here..

The second parameter is the datatype that you would like the value to be casted as.  Then I always aliases the field to something relevant.

Although parsing XML can be a little confusing in SQL Server, it is very powerful.  This is a great way to pass bulk data to a stored procedure from any type of client application.