SIGN UP MEMBER LOGIN:    
ARTICLE

Output Clause in SQL Server

Posted by Dhananjay Kumar Articles | SQL Server 2012 June 16, 2011
Here you will see the Output clause for SQL Server.
Reader Level:


The Output clause gives access to Inserted, updated or deleted rows of a table. In SQL Server 2005 this was possible only through Triggers.

Note: In the following, I am going to use a School Database. You can download the script for it here.

Imagine a scenario that while inserting a value in a Person table, for audit purposes you need to insert a value for the FirstName column and an insertion date in an audit table. This was possible in SQL Server through Triggers. Whereas in SQL Server 2008 we can do this using an Output clause.

I created a table called Audit_Person in a school database.

SqlClause1.gif

Now while inserting in the Person table, I need to insert a row in this table also.

SqlClause2.gif

In the above statement notice that we are fetching the inserted first name using Inserted.FirstName and current date using GetDate and inserting them into the Audit_Person table.

Let us say you have another scenario in which:

  1. You want to delete all the persons with a First Name starting with P
  2. Insert the First Names of deleted rows along with the deleted date into the Audit_Person table

To write that query you can use an Output clause.
SqlClause3.gif

The scripts used in the above explanation are below. Feel free to use them.
 
Create  table Audit_Person
         (PersonName nvarchar(max),
          DateOfEntry date);

Insert into Person(LastName ,FirstName)
   
Output Inserted.FirstName,
          
GetDate()
   
into Audit_Person
   
values
     ('Dave','Pinal');
    

Delete from Person
 
Output deleted.Firstname ,
         
GetDate()
  
Into audit_person
 
where FirstName like 'P%';

I hope this post was useful. Thanks for reading 
 

Login to add your contents and source code to this article
share this article :
post comment
 
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Become a Sponsor