Scott on Writing

Musings on technical writing...

Saturday, October 11, 2008 #

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 @ 12:00 PM | Feedback (2)

My Links

Ads Via DevMavens

Archives

Post Categories

 

I am a Microsoft MVP for ASP.NET.
I am an ASPInsider.
<October 2008>
SMTWTFS
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

Comment Stats

DayTotal% of Total
Sunday 1986.9%
Monday 40314.1%
Tuesday 48216.8%
Wednesday 53118.5%
Thursday 55619.4%
Friday 51918.1%
Saturday 1776.2%
Total 2866100.0%

Hour1Total% of Total
12:00 AM 712.5%
1:00 AM 762.7%
2:00 AM 652.3%
3:00 AM 772.7%
4:00 AM 612.1%
5:00 AM 1154.0%
6:00 AM 1113.9%
7:00 AM 1645.7%
8:00 AM 1806.3%
9:00 AM 1545.4%
10:00 AM 1786.2%
11:00 AM 1846.4%
12:00 PM 1966.8%
1:00 PM 1796.2%
2:00 PM 1625.7%
3:00 PM 1344.7%
4:00 PM 1133.9%
5:00 PM 1003.5%
6:00 PM 953.3%
7:00 PM 1033.6%
8:00 PM 893.1%
9:00 PM 832.9%
10:00 PM 853.0%
11:00 PM 913.2%
Total 2866100.0%

Comments by Blog Entry Date/Time

Day Entry MadeAvg.Total
Sunday 5.18145
Monday 5.10362
Tuesday 4.28462
Wednesday 7.51653
Thursday 6.70643
Friday 5.32431
Saturday 5.00170
Total 5.682866

Hour1 Entry MadeAvg.Total
12:00 AM 5.0035
1:00 AM 1.002
5:00 AM 0.000
7:00 AM 6.3338
8:00 AM 4.72118
9:00 AM 6.04284
10:00 AM 6.12257
11:00 AM 4.23186
12:00 PM 6.75344
1:00 PM 3.05122
2:00 PM 5.29222
3:00 PM 8.60301
4:00 PM 3.7694
5:00 PM 5.86164
6:00 PM 4.56114
7:00 PM 9.15183
8:00 PM 8.42160
9:00 PM 5.00115
10:00 PM 6.3395
11:00 PM 4.5732
Total 5.682866

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


Blog Stats

Favorite Web Sites

My Books

My MSDN Articles