MS SQL Server
  Home arrow MS SQL Server arrow 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  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
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


    (Page 1 of 4 )

    Triggers are procedures that are similar to events, but they have some unique qualities. This article will show you the different types of triggers, explain how to manage them, and then make trigger design recommendations. The first of several parts, it 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).

    Triggers are a unique type of procedure. They are very similar to events—a type of procedure in certain programming languages such as Visual Basic. Events in Visual Basic are initiated by the system when certain actions occur (for instance, a form is loaded, a text box receives focus, or a key is pressed). Triggers are associated with SQL Server objects (such as tables) and executed by SQL Server when a specific change occurs.

    In the following sections, we’ll first examine conceptually different types of triggers: the classic (After) triggers, the Instead-of triggers, and finally DDL triggers. In the remaining sections, we’ll discuss methods for managing triggers from Transact-SQL code and in Management Studio, and then finally discuss trigger design recommendations.

    DML Triggers

    SQL Server 7.0 and earlier versions recognized only one type of trigger. In SQL Server 2000 and SQL Server 2005, this type is called an After trigger. SQL Server 2000 introduced a new type—the Instead-of trigger. Both types are fired on the same types of modification statements:

    • Insert
    • Update
    • Delete

    Since these statements are considered Data Modification Language (DML), After triggers and Instead-of triggers are jointly called DML triggers.

    Physical Design of After Triggers

    The following is the simplified syntax for implementing the core functionality of After triggers:

    Create Trigger trigger_name
    On table
    {After { [Delete] [,] [Insert] [,] [Update] }
      As
        sql_statement [...n]

    As a stored procedure, a trigger logically consists of

    • A header, which is a Transact-SQL statement for creating a trigger. It consists of three components:
      • The name of the trigger
      • The name of the table with which the trigger will be associated
      • A modification statement (that is, an event) that will initiate the trigger
    • A body, which contains Transact-SQL statement(s) to be executed at runtime.

    The following example first creates a new table called MyEquipment, then populates it with Make and Model information from the Equipment table, and finally creates a trigger. The trigger is named trMyEquipment_D and is associated with the MyEquipment table. It is fired after a Delete statement is executed against the table. Its function is very simple—it notifies the user regarding actions and the number of records that have been deleted.

    Create Table dbo.MyEquipment
         (Id int identity,
         Description varchar(500))
    GO

    -- populate table
    Insert dbo.MyEquipment(Description)
         Select top 5 Make + ' ' + Model from dbo.Equipment
    GO

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

    To execute the trigger, you need to execute the Delete statement:

    Delete dbo.MyEquipment
    Where Id = 2

    SQL Server returns the following:

    You have just deleted 1 record(s)!

    (1 row(s) affected)

    You can also execute the Delete statement to delete multiple records:

    Delete dbo.MyEquipment

    Even in this case, the trigger will not be fired once for each record. You will receive just one message:

    You have just deleted 4 record(s)!

    (4 row(s) affected)

    For this reason, it is important to design your trigger to handle actions against multiple records. You will see more reasons in following paragraphs.

    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

    - Windows Server 2008 as a Workstation OS
    - An Overview of Windows Server 2008 R2
    - LINQ to MySQL, Oracle and PostgreSQL Provide...
    - 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





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