MS SQL Server
  Home arrow MS SQL Server arrow Page 5 - 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 
Dedicated Servers 
Moblin 
JMSL Numerical Library 
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: 4 stars4 stars4 stars4 stars4 stars / 7
    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 UPDATE statement


    (Page 5 of 5 )

    The UPDATE statement internally performs two operations (deletion and insertion). So the OUTPUT clause needs to handle both of these two operations. Let us go through the following example.

    Use Northwind
     
    Declare @upd as table(UpRegionID int,UpRegionDescription nchar(50),UpNewRegionDescription nchar(50))
     
    Update region
    Set RegionDescription='NorthernWest'
    Output inserted.regionid,deleted.regiondescription,inserted.
    regiondescription
    into @upd
    Where regionid=6
     
    --Display Newly Updated Value will be displayed
    Select * from @upd
     
    --Displays all values along with newly Updated value will be displayed
     
    Select * from Region

    The above script is very easy to understand and looks similar to the previous examples.  We are declaring a variable “@upd,” which is of type “table” having three fields, “UpRegionID” (of type “int”), “UpRegionDescription” (of  type “nvarchar”) and “UpNewRegionDescription” (of type nvarchar).  After that, we are updating a row into the table "region" by using the “Update” statement.   Let us take a clear look at the “Update” statement:
    Update region
    Set RegionDescription='NorthernWest'
    Output inserted.regionid,deleted.regiondescription,inserted.
    regiondescription
    into @upd
    Where regionid=6

    The column of a particular row being updated into the “region” table must be “cached” using the OUTPUT clause into the virtual table “@upd.”  Here “UPDATE” means deleting the previous value from that column and inserting (or replacing) the new value into that column.  According to the syntax of the UPDATE statement, the “OUTPUT” clause needs to be after the “SET” expression. 

    According to my needs, once the UPDATE statement is executed successfully, I would like to have both old and new values (of the same column “RegionDescription”) be stored in the virtual table “@upd.”  Since we are updating here, we need to use both reserved words “inserted” and “deleted” along with the OUTPUT clause.

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

    How about specifying more than one column name for the UPDATE statement?  The following example illustrates the same.

    Use Northwind
     
    Declare @updCategory as table(UpCategoryID int,UpCategoryName nvarchar(15),NewUpCategoryName nvarchar(15),UpDescription ntext,NewUpDescription ntext)
     
    Update categories
    Set CategoryName='DairyProducts',Description='Cheese'
    Output inserted.categoryid,deleted.categoryname,inserted.categoryname,
    deleted.description,inserted.description
    into @updCategory
    where categoryid=4
     
    --Display Newly Updated Value will be displayed
    Select * from @updCategory
     
    --Displays all values along with newly Updated value will be displayed
     
    Select * from Categories

    The above example is similar to the previous one except for the difference in the UPDATE statement. In the previous example, we didn’t mention more than one column for the UPDATE statement. But here we mentioned more than one column name. Now all the updates get stored in the cache (or virtual table).  The rest is very similar to the previous example.

    All the examples in this article were tested using SQL Server 2005 Enterprise Edition. Please note that I didn’t really test the examples on any of the other versions/editions of the similar suite of Microsoft products. 

    Any comments, suggestions, ideas, improvements, bugs, errors, feedback etc. are highly appreciated at jag_chat@yahoo.com.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · 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 1 hosted by Hostway