Preview Content - log in or Buy Access Pass

T-SQL Output Clause

You might often perform deletes, inserts and updates using T-SQL and might have requirements to re-use the affected rows. With SQL Server 2008 and 2008 R2 you can easily do that using TSQL OUTPUT clause. OUTPUT gives you access to deleted, Inserted and Updated rows affected by your standard TSQL Code so you can retrieve old values . Below we created several examples each giving simple example for insert, delete and update.

Below are several examples when you could use OUTPUT Clause:

1) Let's say you want to archive (move) data which is 90 days old.
2) You want to record field value changes.
3) Or simply want to get IDENTITY field ID eg. Insert row and recover id autogenerated by sql
Typical questions are:

  • How to get affected inserted rows. Tsql output into inserted rows
  • How to get affected deleted rows. T-sql output into delete
  • How to get affected updated rows. Tsql ouput into update

Syntax is difficult to read so I'm not going to post it here basically when you use Insert Into, Delete From or Update you can add OUTPUT clause and specify values using Inserted.Column, Deleted.Column. Update is slightly different and you access OLD value using Deleted.Column and NEW value using Inserted.column. After that you just put INTO DestinationTable (columnA, columnB) or @TableVariable. Specifying column names is optional but good practise.

Enough of theory let's learn on examples!

To access members only content...
Log in (active member) or buy Access Pass
Learn as much as you like
Unlimited learning access pass*
* Secure one off payment, no re-occuring payments, no surprises
T-SQL SSIS SSRS 79 Mini Courses 529 Pages 287 Videos 314 Articles
Forum Live Webinars 12 Webinar Recordings DIY Projects Progress Tracking
New member joins every 8 hours

60 days, 100% money back guarantee
NO risks, NO questions asked, NO hard feelings

I can't speak highly enough of the quality MSBI materials industry practitioners with varying level of experience and exposure can benefit from this website. Their structured learning plan and courses, DIY projects, webinars and relevant resources go a very long way. I've gained a whole new perspective on delivering client BI projects to highest standards by employing best practices recommended on this website. Best quality MSBI training online suite I have used and least expensive too.

Franklin Demilade Osinowo

Best and affordable BI Training platform in the web. KEBI Academy doesn't provide training on BI rather their approach is to make you learn BI by yourself. Self Learning is always the best approach and I recommend KEBI Academy.

Jaiyaram Mahendran

You both are doing a great job by sharing your knowledge and experience, it helps many people who want to learn MSBI from the starting point and also for an experienced person to get to know more about MSBI. Thank you and keep sharing your MSBI knowledge and experience.

Shweta Patel

Great work on the videos. I have been enjoying them.

Patt Nelson

Did you find this page useful?
+5  |  -0
(5 Votes)