Web Analytics Made Easy - Statcounter
Output Clause:- The SQL SERVER 2005 gives us an Output Clause which gives us the information about each row affected by the Insert, Update , Delete and Merge statement. It is more useful than @@scope_Identity and @@Identity column since these global variables gives us the information about the last inserted identity column value, but the Output clause gives us the last affected identity in case of Inserted, deleted, updated and merge statement. Output clause has the access to the Magic tables (Inserted and deleted) and we can capture the records of affected rows into the temporary tables too for latter use.

Create table Tbl_output (id int identity(1,1), fname nvarchar(100), lname nvarchar(100))

Example of Output clause in case of Insert command

Declare @outputtemp Table  (id int , fname nvarchar(100), lname nvarchar(100))

Insert into tbl_output(fname,lname)
Output inserted.id, inserted.fname, inserted.lname into @outputtemp
values(‘Vivek’, ‘Johari’)

Select * from @outputtemp

Example of Output clause in case of Update command

Update tbl_output set fname=’Vibodh’ 
Output inserted. fname , deleted.fname
where id=1

Example of Output clause in case of Delete command

Delete from tbl_output 
Output deleted.* 
where id=1

Read more about Output Clause


Discover more from Technology with Vivek Johari

Subscribe to get the latest posts sent to your email.

By vivekjohari

I am currently working as a Senior Database Professional and have around 18 years of experience in database. Degree:- Master Degree in Computer(MCA) Certification course in Data Science & Machine Learning from Indian Institute of Technology (IIT), Delhi Work experience:- Designing of the database. Database Optimization. Writing Complex Stored Procedures,Functions,Triggers etc. Designing and developing SSIS & DTS packages. Designing SQL Reports using SSRS. Database Server Maintenance. Certification:- MCTS: DA-100: Analysing Data with Microsoft Power BI MCTS: DP-300: Administering Relational Databases on Microsoft Azure Microsoft certified Sql DBA in Sql server 2008 (MCTS). Microsoft certified BI professional in Sql server 2008 (MCTS). Oracle certified profession DBA in ORACLE 10g (OCP) certified profession DBA in ORACLE 9i (OCP) My other publication Technical Blog:- Technologies with Vivek Johari Guest Author and Blogger at sqlservercentral.com

28 thought on “SQL Server- Output clause”

Leave a Reply

Discover more from Technology with Vivek Johari

Subscribe now to keep reading and get access to the full archive.

Continue reading