MS SQL Server
  Home arrow MS SQL Server arrow Page 3 - Brief Introduction to Triggers in SQL Serv...
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 
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

Brief Introduction to Triggers in SQL Server 2000
By: Mayank Gupta
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 91
    2004-06-30

    Table of Contents:
  • Brief Introduction to Triggers in SQL Server 2000
  • After Triggers
  • Multiple After and Instead Of Triggers
  • Mixing Trigger Types

  • 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


    Brief Introduction to Triggers in SQL Server 2000 - Multiple After and Instead Of Triggers


    (Page 3 of 4 )

    Multiple After Triggers

    More than one trigger can now be defined on a table for each Insert/Update/Delete. Although in general you might not want to do this (it's easy to get confused if you over-use triggers) there are situations where this is ideal. One example that springs to mind is that you can split your triggers up into two categories:

    • Application based triggers (cascading deletes or validation for example)
    • Auditing triggers (for recording details of changes to critical data)

    This would allow you to alter triggers of one type without fear of accidentally breaking the other.

    If you are using multiple triggers, it is of course essential to know which order they fire in. A new stored procedure called sp_settriggerorder allows you to set a trigger to be either the "first" or "last" to fire.

    If you want more than two triggers to fire in a specific order, there is no way to specifically define this. A deeply unscientific test I did indicated that multiple triggers for the same table and operation will run in the order they were created unless you specifically tell them otherwise. I would not recommend relying on this though.

    Instead Of Triggers

    Instead Of Triggers fire instead of the operation that fires the trigger, so if you define an Instead Of trigger on a table for the Delete operation, they try to delete rows, and they will not actually get deleted (unless you issue another delete instruction from within the trigger) as in this simple example:

    CREATE TABLE Mayank (Name  varchar(32))
    GO

    CREATE TRIGGER tr_mayank ON Mayank
    INSTEAD OF DELETE
    AS
        PRINT 'Sorry - you cannot delete this data'
    GO

    INSERT Mayank
        SELECT 'Cannot' union
        SELECT 'Delete' union
        SELECT 'Me'
    GO

    DELETE Mayank
    GO

    SELECT * FROM Mayank
    GO
    DROP TABLE Mayank

    If you were to print out the contents of the Inserted and Deleted tables from inside an Instead Of trigger you would see they behave in exactly the same way as normal. In this case the Deleted table holds the rows you were trying to delete, even though they will not get deleted.

    Instead of Triggers can be used in some very powerful ways!

    • You can define an Instead Of trigger on a view (something that will not work with After triggers) and this is the basis of the Distributed Partitioned Views that are used so split data across a cluster of SQL Servers.
    • You can use Instead Of triggers to simplify the process of updating multiple tables for application developers.

    More MS SQL Server Articles
    More By Mayank Gupta


     

    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