MS SQL Server
  Home arrow MS SQL Server arrow Page 4 - Wonders of the OUTPUT Clause in SQL Server...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
MS SQL SERVER

Wonders of the OUTPUT Clause in SQL Server 2005
By: Jagadish Chaterjee
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 9
    2006-05-08

    Table of Contents:
  • Wonders of the OUTPUT Clause in SQL Server 2005
  • OUTPUT clause with single INSERT statement
  • OUTPUT clause with multiple INSERT statements
  • OUTPUT clause with single DELETE statement
  • OUTPUT clause with single UPDATE statement

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Wonders of the OUTPUT Clause in SQL Server 2005 - OUTPUT clause with single DELETE statement


    (Page 4 of 5 )

    Let us go through the following example, which shall delete a single row from the table and display the result back onto the screen.

    Use Northwind
     
    Declare @del as table(delRegionID int,delRegionDescription nchar(50))
     
    Delete  region
    Output deleted.regionid,deleted.regiondescription
    into @del
    Where regionid=7
     
    --Display Deleted Value will be displayed
     
    Select * from @del

    The above script is very easy to understand.  We are declaring a variable “@del,” which is of type “table” having two fields, “delRegionID” (of type “int”) and “delRegionDescription” (of  type “nvarchar”).  After that, we are deleting a row from the table "region" by using the “Delete” statement.   Let us take a clear look at the “Delete” statement:

    Delete  region
    Output deleted.regionid,deleted.regiondescription
    into @del
    Where regionid=7

    The row being deleted from the “region” table must be “cached” using the OUTPUT clause into the virtual table “@del.” For that reason, we inserted the “OUTPUT” clause, just before the “Where” clause.  All the values which are about to be deleted will also have a copy to be saved in our virtual table “@del.”  As we are using the DELETE statement here, we need to use the reserved word “deleted.”

    Finally, we execute the SELECT statement on the virtual table “@del,” which directly retrieves all the values cached and displays them on the screen.

    Let us go through another example where we are deleting more than one row.

    Use Northwind
     
    Declare @del as table(delRegionID int,delRegionDescription nchar(50))
     
    Delete  region
    Output deleted.regionid,deleted.regiondescription
    into @del
    Where regionid >4
     
    --Display Deleted Value will be displayed
    Select * from @del

    If you can clearly observe the above script you must understand that the “where” clause looks a bit different.  It works with more than one row now! We are trying to delete more than one row at a time, and it in turn saves the deleted information in the virtual table “@del.” 

    Finally, we execute the SELECT statement on the virtual table “@del,” which directly retrieves all the values cached and displays them on the screen.

    More MS SQL Server Articles
    More By Jagadish Chaterjee


       · Hello guys! This is an introductory to OUTPUT clause in SQL Server 2005. Try it...
       · Thanks
       · Hi, Nice article, but I was wondering if you have any hints on how to use this...
     

    MS SQL SERVER ARTICLES

    - Completing the Introduction to Transact-SQL
    - A Brief Introduction to Transact-SQL
    - Lookups and Blocking Bad Data
    - Field Validation Rules for Blocking Bad Data
    - Using Masks to Block Bad Data
    - Blocking Bad Data
    - Using @@ROWCOUNT and TABLE Variables for Dat...
    - How to Use Variables, IF and CASE in Databas...
    - Creating Important Aspects of Notification S...
    - Working wth Variables in Database Interactio...
    - Delving Deeper into Notification Services
    - Notification Services
    - Building a Multi-table Report with SQL 2005 ...
    - A Secure Way of Building Connection Strings
    - Transferring a Database Using the SSIS Desig...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT