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

Triggers
By: McGraw-Hill/Osborne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 8
    2006-08-31

    Table of Contents:
  • Triggers
  • Inserted and Deleted Virtual Tables
  • What Triggers a Trigger?
  • Full Syntax of After Triggers

  • 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


    Triggers - Inserted and Deleted Virtual Tables


    (Page 2 of 4 )

    SQL Server maintains two temporary virtual tables during the execution of a trigger: Deleted and Inserted. These tables contain all the records inserted or deleted during the operation that fired the trigger. You can use this feature to perform additional verification or additional activities on affected records.

    You are probably wondering if there is an Updated table. No. Because an Update can be performed as a combination of the Delete and Insert statements, records that were updated will appear in both the Deleted and Inserted tables.

    SQL Server does not create both tables in all cases. For example, in a trigger fired during a Delete statement, only a Deleted virtual table is accessible. A reference to an Inserted virtual table will cause an error.

    The following table summarizes the presence of virtual tables in the relevant Transact-SQL statements:

    Modification Statement

    Deleted

    Inserted

    Insert

    N/A

    New records

    Update

    Old version of updated records

    New version of updated records

    Delete

    Deleted records

    N/A

    The following modifies the trigger from the previous section to display which records are deleted:

    Alter Trigger trMyEquipment_D
    On dbo.MyEquipment
    After Delete     -- For Delete
    As
         Select 'You have just deleted following '
              + Cast(@@rowcount as varchar)
              + ' record(s)!'

         Select * from deleted
    go

    When you delete all records from the MyEquipment table, SQL Server returns the following:

    -------------------------------------------
    You have just deleted following 5 record(s)!

    (1 row(s) affected)

    Id          Description
    ----------- -------------------------------1           Toshiba Portege 7020CT
    2           Sony Trinitron 17XE
    3           NEC V90
    4           HP LaserJet 4
    5           HP LaserJet 4

    (5 row(s) affected)

    You can use values from these tables, but you cannot modify them directly. If you need to perform some operation on records that were inserted, for example, you should not try to change them in the Inserted table. The proper method would be to issue a regular Transact-SQL statement against the original table. In the Where or From clause, you can reference the virtual table (Inserted) and in that way limit the subset of the original table that you are targeting.

    In the following example, the trigger calculates a SOUNDEX code for the Make and Model of the Equipment records affected by the Insert or Update statement that has fired the trigger:

    Alter Trigger trEquipment_IU
    On dbo.Equipment
    After Insert, Update   -- For Insert, Update
    As
         -- precalculate ModelSDX and MakeSDX field
         -- to speed up use of SOUNDEX function
         update dbo.Equipment
         Set ModelSDX = SOUNDEX(Model),
              MakeSDX = SOUNDEX(Make)
         where EqId IN (Select EqId from Inserted)

    More MS SQL Server Articles
    More By McGraw-Hill/Osborne


       · This article is an excerpt from the book "Microsoft SQL Server 2005 Stored Procedure...
       · nice article and exlent presentation.i am very thankful to you
       · Glad you like it; there's more coming.
       · Its Reaaly Useful And Nice Topic.Concepts Explained Very Clearly With Simple And...
       · Great article. Very, very helpful.
     

    Buy this book now. This article is excerpted from chapter nine of the book Microsoft SQL Server 2005 Stored Procedure Programming in T-SQL and .NET, written by Dejan Sunderic (McGraw-Hill/Osborne, 2006; ISBN: 0072262281). Check it out today at your favorite bookstore. Buy this book now.

    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