Click here to Skip to main content
15,868,016 members
Articles / Database Development / SQL Server / SQL Server 2012
Tip/Trick

SQL Server: Changed data capture without using Triggers

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
23 Oct 2012CPOL 10.4K   3   1
How to use DELETED and INSERTED virtual tables to capture data change in result of any INSERT, UPDATE or DELETE statement.

Introduction

Most SQL Server users think that a trigger is the only place where we can use DELETED and INSERTED virtual tables to capture data change in result of any INSERT, UPDATE or DELETE statement.

Old and new data values affected by DML operations can be captured by using OUTPUT clause along with virtual DELETED and INSERTED tables. Here are few examples which will be helpful to understand usage of this OUTPUT clause in simple insert, update and delete queries. 

Update

SQL
USE AdventureWorks
GO
-- Create a table variable to hold updated rows
DECLARE @UpdatedRecords TABLE
(AddressID INT, OldAddressLine2 VARCHAR(50), NewAddressLine2 VARCHAR(50))

UPDATE Person.Address
SET AddressLine2 = 'Silver Street new'
OUTPUT DELETED.AddressID, DELETED.AddressLine2,INSERTED.AddressLine2
INTO @UpdatedRecords
WHERE AddressID = 3

SELECT * FROM @UpdatedRecords

Delete

SQL
USE AdventureWorks
GO
-- Create a table variable to hold deleted rows
DECLARE @DeletedRecords TABLE
(AddressID INT, AddressLine1 VARCHAR(50), AddressLine2 VARCHAR(50)
 ,City VARCHAR(50),StateProvinceID INT,PostalCode VARCHAR(10)
,rowguid UNIQUEIDENTIFIER,ModifiedDate DATETIME)

DELETE FROM Person.Address
OUTPUT DELETED.*
INTO @DeletedRecords
WHERE AddressID = 3

SELECT * FROM @DeletedRecords

Insert

SQL
USE AdventureWorks
GO
-- Create a table variable to hold deleted rows
DECLARE @InsertedRecords TABLE
(AddressID INT, AddressLine1 VARCHAR(50), AddressLine2 VARCHAR(50)
 ,City VARCHAR(50),StateProvinceID INT,PostalCode VARCHAR(10)
,rowguid UNIQUEIDENTIFIER,ModifiedDate DATETIME)

INSERT INTO Person.Address
(AddressLine1 , AddressLine2 ,City ,StateProvinceID ,PostalCode
 ,rowguid ModifiedDate)
OUTPUT INSERTED.*
INTO @InsertedRecords
VALUES ('3rd BlackStone rd','wst.51','Bothell',78,98010,NEWID(),GETDATE()
SELECT * FROM @InsertedRecords

Note: This method is applicable for SQL Server 2005 and above versions.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader CureMD
Pakistan Pakistan
Aasim Abdullah is working as SQL Server DBA with CureMD (www.curemd.com) based in NY, USA. He has been working with SQL Server since 2007 (Version 2005) and has used it in many projects as a developer, administrator, database designer. Aasim's primary interest is SQL Server performance tuning. If he finds the time, he like to sketch faces with graphite pencils.

Comments and Discussions

 
GeneralMy vote of 5 Pin
jfln9-Aug-13 6:48
jfln9-Aug-13 6:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.