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.
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!
1) Archive Data. Move all Orders which have been placed over 90 days ago into Archived table.
DELETE FROM OrderTable
WHERE DATEDIFF(D, MyDateField, GETDATE())>30
2) Record field value change. We will record Employee Salary change in this example.
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)
INTO @InsertedRow (ID)
We need you!
By becoming a proud member we can:
Become a proud member
- Switch off annoying ads
- Create more articles and videos
- Develop more useful features supporting your learning
- Provide FREE webinars (for members)
- Feel our hard work since 2011 is appreciated
and get exclusive features.
If 2% of our 64,943 visitors (Last 30 days) become a proud member than we can quit our jobs and work full time on this website.
We started membership in May 2014 and so far 0.07% of our visitors are members and we would like to say THANK YOU to those who become a proud member and support our efforts.
Thanks to members:
- Education is for FREE and we create more FREE articles and videos
- We provide new features (members) that can speed up learning.
- 53% of our page views (82,000 each month) do not have page ads.
- We switched video ads from 14% of our videos (50 out of 345).
We have invested 4 years of our time in this website and we estimate it will take about 5 years before we can quit our jobs and dedicate all our time
to develop considerably our website which should help to truly achieve our vision of free education for everyone; all thanks to support of our members only.
Become a proud member