Scott on Writing

Musings on technical writing...

OUTPUTing Data from the Just-Inserted, Updated, or Deleted Row(s)

Between work and diaper changes I've been reading Michael Coles's book Pro T-SQL 2008 Programmer's Guide, and found this little gem (pg. 527-528):

The OUTPUT Clause
You can use the OUTPUT clause with the the INSERT, UPDATE, DELETE and MERGE DML statements. ... The OUTPUT clause returns information about the rows affected by the DML statements that can be useful in comparing preupdate and postupdate data, or for troubleshooting and logging purposes. ... You can use the OUTPUT clause to output a SQL result set like that returned by a SELECT statement, or you can combine OUTPUT with the INTO clause to output rows to a table or a table variable.

This feature is supported in T-SQL 2008, but was initially added to Microsoft SQL Server 2005. And here it is three years later and I'm just learning about it!

One use of the OUTPUT clause is to grab the just-inserted IDENTITY column value:

INSERT INTO TableName(ColumnList)
OUTPUT inserted.IdentityColumnName
VALUES(Values)

The above will return the just-inserted IDENTITY value as a result set, just as if you had followed an OUTPUT-less INSERT statement with the statement:

SELECT SCOPE_IDENTITY()

You could use the OUTPUT statement to return information about the rows affected by an UPDATE. For example, in these tough economic times you might need to increase prices by 20% for all products that cost less than $10.00. The following statement performs the described update and returns those products whose prices were increased, showing both their old price and their new price:

UPDATE Products SET
    Price = Price * 1.20
OUTPUT inserted.ProductID, deleted.Price AS OldPrice, inserted.Price AS NewPrice
WHERE Price < 10.00

This UPDATE statement will modify the data and return a three-column result set with a row for each modified product along with its preupdate price (deleted.Price) and its post-update price (inserted.Price).

Pretty neat, eh?

In fact, You can string these DML statements together, so you do an UPDATE with an OUTPUT whose results are then automatically INSERTed into another table (such as an audit table). The OUTPUT statement feels like in-place triggers (kind of like how Common Table Expressions are akin to in-place views).

I'll have to write an article on the OUTPUT statement on 4Guys one of these days...

Further Reading....

posted on Saturday, October 11, 2008 12:00 PM

Feedback

# Interesting Finds: 2008.10.13~2008.10.15 10/14/2008 6:34 PM gOODiDEA.NET

Other Is it important to write good code? Must Have Software 2008 Edition Database OUTPUTing Data from

# OUTPUTing Data from the Just-Inserted, Updated, or Deleted Row(s) 10/19/2008 3:53 AM DotNetKicks.com

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# SQL Server Blog by Jason Massie 6/7/2009 12:01 PM Pingback/TrackBack

SQL Server Blog by Jason Massie

Title:  
Name:  
Url:
Protected by Clearscreen.SharpHIPEnter the code you see:
Comments   

My Links

Ads Via DevMavens

Archives

Post Categories

 

I am a Microsoft MVP for ASP.NET.
I am an ASPInsider.
<November 2009>
SMTWTFS
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345

Comment Stats

DayTotal% of Total
Sunday 2046.8%
Monday 42514.2%
Tuesday 51017.0%
Wednesday 55118.4%
Thursday 57919.3%
Friday 54318.1%
Saturday 1876.2%
Total 2999100.0%

Hour1Total% of Total
12:00 AM 762.5%
1:00 AM 812.7%
2:00 AM 672.2%
3:00 AM 822.7%
4:00 AM 672.2%
5:00 AM 1254.2%
6:00 AM 1183.9%
7:00 AM 1785.9%
8:00 AM 1926.4%
9:00 AM 1575.2%
10:00 AM 1866.2%
11:00 AM 1936.4%
12:00 PM 2006.7%
1:00 PM 1846.1%
2:00 PM 1685.6%
3:00 PM 1354.5%
4:00 PM 1153.8%
5:00 PM 1063.5%
6:00 PM 1013.4%
7:00 PM 1063.5%
8:00 PM 923.1%
9:00 PM 872.9%
10:00 PM 893.0%
11:00 PM 943.1%
Total 2999100.0%

Comments by Blog Entry Date/Time

Day Entry MadeAvg.Total
Sunday 4.94158
Monday 4.79383
Tuesday 4.13475
Wednesday 7.44677
Thursday 6.24674
Friday 5.02457
Saturday 4.73175
Total 5.412999

Hour1 Entry MadeAvg.Total
12:00 AM 5.2937
1:00 AM 1.002
5:00 AM 0.000
7:00 AM 3.8550
8:00 AM 3.72134
9:00 AM 6.06291
10:00 AM 5.61275
11:00 AM 4.27192
12:00 PM 6.23349
1:00 PM 3.17133
2:00 PM 5.00230
3:00 PM 7.62320
4:00 PM 3.89105
5:00 PM 6.00168
6:00 PM 4.56114
7:00 PM 8.95188
8:00 PM 8.58163
9:00 PM 5.00115
10:00 PM 6.31101
11:00 PM 4.5732
Total 5.412999

Learn More About Comment Stats
1 - All times GMT -8...


Blog Stats

Favorite Web Sites

My Books

My MSDN Articles