Sign In | Feedback | Donate | Search
Katie and Emil Logo
Vote Yes! I like it! [+0]  |  Vote no! Dislike? [-0]
Author: by Katie Glownia
Overview: 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.
Example: 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: 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!
Code Examples: 1) Archive Data. Move all Orders which have been placed over 90 days ago into Archived table.

DELETE FROM OrderTable
     OUTPUT Deleted.*
     INTO OrderTableArchived
WHERE DATEDIFF(D, MyDateField, GETDATE())>30


2) Record field value change. We will record Employee Salary change in this example.

UPDATE Employee
SET Salary = 50000
     OUTPUT Inserted.ID, Deleted.Salary, InsertedSalary, Getdate()
     INTO EmployeeSalaryHistory (ID, OldSalary, NewSalary, DateChanged)
WHERE ID = 7

Note: If the value for a particual field hasn't changed you can access it both using inserted.column and deleted.column


3) Insert row and Get Identity. Some of your may know that @@Identity or Scope_Identity() don't always return identity number we want and OUTPUT is strongly recommended method by Microsoft (See details)

Below I will slightly complicate example by using table variable to store the ID

DECLARE @InsertedRow AS TABLE (ID INT)


INSERT INTO Category (CategoryValue)
     OUTPUT Inserted.ID
     INTO @InsertedRow (ID)
VALUES ('T-SQL')

 

Upsss... Something went wrong and Google Ads on our website did not display.

Google ads sponsor FREE content on our website and without them it would not exist.

How to fix the problem?
  1. Check if the Google Search display any ads by searching the term Business Intelligence
    • If ads don't display, try different search terms and if there no ads then there might be a problem with your browser. If you made changes to your browser behaviour then undoing them will help.
    • If ads display then, refresh our page one more time, if the problem is still there than please contact us.
  2. Become 'No Ads Learner' Member or if you are already one Sign In.
Close
Take care
Katie & Emil
Well done! It seems Google Ads display properly now on our website.

Access our website's FREE content for FREE!