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

Trigger Specifics
By: McGraw-Hill/Osborne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 4
    2006-09-07

    Table of Contents:
  • Trigger Specifics
  • Nested and Recursive Triggers
  • Instead-of Triggers
  • Triggers on Views

  • 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


    Trigger Specifics - Nested and Recursive Triggers


    (Page 2 of 4 )

    A trigger can fire other triggers on the same or other tables when it inserts, updates, or deletes records in them. This technique is called nesting triggers.

    If a trigger changes records in its own table, it can fire another instance of itself. Such an invocation is called direct invocation of recursive triggers.

    There is another scenario in which invocation of recursive triggers might occur. The trigger on one table might fire a trigger on a second table. The trigger on the second table might change the first table again, and the first trigger will fire again. This scenario is called indirect invocation of recursive triggers.

    All these scenarios might be ideal for implementing referential integrity and business rules, but they might also be too complicated to design, understand, and manage. If you are not careful, the first trigger might call the second, then the second might call the first, then the first the second, and so on.

    Very often, the SQL Server environment is configured to prevent this kind of behavior. To disable nested triggers and recursive triggers, you need to use the stored procedure sp_configure to set the Nested Triggers server option, and then use the Alter Table statement to set the Recursive_Triggers option to Off mode. Keep in mind that recursive triggers will be disabled automatically if you disable nested triggers.

    After Trigger Restrictions

    The following are the trigger restrictions, none of which usually causes any difficulties:

    • The trigger must be created with the first statement in a batch.
    • The name of the trigger is its Transact-SQL identifier, and therefore must be no more than 128 characters long.
    • The trigger’s name must be unique in the database.
    • A trigger can only be associated with one table, but one table can have many triggers. In the past, only one trigger could be associated with one modification statement on one table. Now, each required function can be implemented in a separate trigger. By implementing these features in separate triggers, you assure that the triggers will be easier to understand and manage.
    • Triggers cannot be nested to more than 32 levels, nor can they be invoked recursively more than 32 times. Attempting to do so causes SQL Server to return an error.
    • A trigger must not contain any of the following Transact-SQL statements:

      Alter Database          Drop Database

      Alter Procedure         Drop Default

      Alter Table               Drop Index

      Alter Trigger             Drop Procedure

      Alter View                Drop Rule

      Create Database       Drop Table

      Create Default          Drop Trigger

      Create Index            Drop View

      Create Procedure      Grant

      Create Rule              Load Database

      Create Schema         Load Log

      Create Table            Reconfigure

      Create Trigger          Restore Database

      Create View             Restore Log

      Deny                      Revoke

      Disk Init                  Truncate Table

      Disk Resize              Update Statistics
    • You cannot reference text, Ntext, or image columns in triggers, but there are no problems with referencing varchar(max), Nvarchar(max), and varbinary(max) columns:

      CREATE TRIGGER dbo.trOrderHeader_D
      ON dbo.OrderHeader
      AFTER DELETE
      AS
      BEGIN
      SET NOCOUNT ON

      -- collect OrderItems and Orders in OrderDeleted
      INSERT INTO [dbo].[OrderDeleted] 
          ([OrderId],[OrderDate],[RequestedById]
          ,[TargetDate],[CompletionDate],[DestinationLocationId]
          ,[Note],[OrderTypeId],[OrderStatusid]
          ,[UserName],[ChangeDT])  
      SELECT [OrderId],[OrderDate],[RequestedById]
          ,[TargetDate],[CompletionDate],[DestinationLocationId]
          ,[Note],[OrderTypeId],[OrderStatusid]
          , SUSER_SNAME(), GETDATE()
      FROM deleted

      END

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


       · This article is an excerpt from the book "Microsoft SQL Server 2005 Stored Procedure...
     

    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 3 hosted by Hostway