SQL Server
  Home arrow SQL Server arrow Page 3 - Using Triggers In MS SQL Server
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
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? 
SQL SERVER

Using Triggers In MS SQL Server
By: David Rusik
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 476
    2002-01-01

    Table of Contents:
  • Using Triggers In MS SQL Server
  • What exactly is a trigger?
  • UPDATE and DELETE triggers
  • Conclusion

  • 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


    Using Triggers In MS SQL Server - UPDATE and DELETE triggers


    (Page 3 of 4 )

    Now that we understand how an "INSERT" trigger works, let's take a look at "UPDATE" and "DELETE" triggers. Here's an "UPDATE" trigger:

    CREATE TRIGGER trig_updateAuthor

    ON authors

    FOR UPDATE

    AS

    DECLARE @oldName VARCHAR(100)

    DECLARE @newName VARCHAR(100)

    IF NOT UPDATE(au_fName) AND NOT UPDATE(au_lName)

    BEGIN

    RETURN

    END

    SELECT @oldName = (SELECT au_fName + ' ' + au_lName FROM Deleted)

    SELECT @newName = (SELECT au_fName + ' ' + au_lName FROM Inserted)

    PRINT 'Name changed from "' + @oldName + '" to "' + @newName + '"'


    This trigger would automatically be executed whenever we updated one/more records in the "authors" table. It starts out by creating two new variables: oldName and newName. The "UPDATE" function is used to check whether or not the "au_fName" and "au_lName" fields have been updated by the "UPDATE" query that executed the "trig_updateAuthor" trigger. If both fields haven't, then the trigger returns control to SQL server.

    As I already mentioned, "UPDATE" triggers have access to two virtual tables: Deleted (which contains all of the fields and values for the records before they were updated), and Inserted (which contains all of the fields and values for the records after they have been updated). We get the value of the users name before the update from the "Deleted" table and store it in the "oldName" variable.

    The updated name is stored in the "newName" variable, and is extracted from the virtual table, "Inserted". Lastly, both the authors name before and after the update query are printed.

    So, if we ran an update query (through Query Analyzer) like this:

    UPDATE authors

    SET au_lName = 'Black'

    WHERE au_id = '172-32-1176'


    ... then Query Analyzer would display the following text in the results pane:

    Name changed from "John Doe" to "John Black"

    Update triggers can also be used to check field constraints and relationships. The "contract" field of the "authors" table is a bit field representing whether or not this author has a contract with their publisher. The publisher may require notification of when an author who is on contract is removed from the "authors" table.

    We could create a "DELETE" trigger on the "authors" table that would do this for us automatically:

    CREATE TRIGGER trig_delAuthor

    ON authors

    FOR DELETE

    AS

    DECLARE @isOnContract BIT

    SELECT @isOnContract = (SELECT contract FROM Deleted)

    IF(@isOnContract = 1)

    BEGIN

    PRINT "Code to notify publisher goes here"

    END


    The "DELETE" trigger follows the same format and keyword syntax as the "INSERT" and "UPDATE" triggers. The only difference is that the "DELETE" trigger has access to the virtual table "Deleted", which contains all of the deleted rows from the "DELETE" command that triggered the "trig_delAuthor" trigger in the first place.

    More SQL Server Articles
    More By David Rusik


       · >> When this is the case, the "UPDATE" trigger is called for each row that was>>...
       · Its really very helpfull for the beginners to learn it
       · >> When this is the case, the "UPDATE" trigger is called for each row that was>>...
       · >> When this is the case, the "UPDATE" trigger is called for each row that was >>...
       · i have faced the same problem where i get an error which says multiple rows selected...
       · its very helpful
       · Very helpful for new ppl learning sql serverThanks,
       · A)once triggers are called after specific times of insert command executions of...
     

    SQL SERVER ARTICLES

    - Executing SQL Server Stored Procedure from P...
    - How to Search for Date and Time Values Using...
    - Replication: SQL Server 2000 - Part 2
    - Replication: SQL Server 2000 - Part 1
    - SQL Sever: Storing Code in Binary or Text Fi...
    - Execute SQL on Multiple Tables/Columns - New...
    - How to Connect to a SQL Server from Visual F...
    - SQL Server Hardware Tuning and Performance M...
    - Primary Key on Multiple Tables – New RDBMS C...
    - Migrating from Sybase to SQL Server
    - What's Best for DBAs? GUI or T-SQL Comma...
    - How to Perform a SQL Server Performance Audit
    - An Introduction To The Bulk Copy Utility
    - SQL Server Stored Procedures 101
    - Building Your First SQL Server 2000 Database







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek