MS SQL Server
  Home arrow MS SQL Server arrow Page 2 - 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 INSERT statement


    (Page 2 of 5 )

    Let us go through the following example, which shall insert a single row into the table and display the result on the screen.

    Use Northwind
     
    Declare @ins as table(InsRegionID int,InsRegionDescription nchar(50))
     
    Insert Region
    Output inserted.regionid,inserted.regiondescription into @ins
    values(5,'NorthEast')
     
    --Display Newly Inserted Value will be displayed
    Select * from @ins
     
    --Displays all values along with newly Inserted value will be displayed
     
    Select * from Region

    The above script is very easy to understand.  We are declaring a variable “@ins,” which is of type “table” having two fields, “InsRegionID” (of type “int”) and “InsRegionDescription” (of  type “nvarchar”).  After that, we are inserting a row into the table "region" by using the “Insert” statement.   Let us take a clear look at the “Insert” statement:

    Insert Region
    Output inserted.regionid,inserted.regiondescription into @ins
    values(5,'NorthEast')

    The above statement has its own details to understand.  The row being inserted into the “region” table must be “cached” using the OUTPUT clause into the virtual table “@ins.”  For that reason, we inserted the “OUTPUT” clause, just before the “values” clause.  All the values which are going to be inserted will also have a copy to be saved in our virtual table “@ins.”  As we are using an INSERT statement here, we need to use the reserved word “inserted.”

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

    How about specifying explicit column names for the INSERT statement?  The following example illustrates the same.

    Use Northwind
     
    Declare @ins as table(InsRegionID int,InsRegionDescription nchar(50))
    Insert Region(RegionID,RegionDescription)
    Output inserted.regionid,inserted.regiondescription into @ins
    values(6,'SouthNorth')
     
    --Display Newly Inserted Value will be displayed
    Select * from @ins
     
    --Displays all values along with newly Inserted value will be displayed
     
    Select * from Region

    The above example is similar to the previous one except for the difference in the INSERT statement. In the previous example, we didn’t mention any explicit column names for the INSERT statement, but here we mentioned explicit column names. Even though it is not compulsory to work with explicit column names, it is highly recommended that you include them as part of the INSERT statement.

    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 1 hosted by Hostway
    Stay green...Green IT