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

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 / 88
    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 - After Triggers


    (Page 2 of 4 )

    Triggers that run after an update, insert, or delete can be used in several ways:

    • Triggers can update, insert, or delete data in the same or other tables. This is useful to maintain relationships between data or to keep audit trail information.
    • Triggers can check data against values of data in the rest of the table or in other tables. This is useful when you cannot use RI constraints or check constraints because of references to data from other rows from this or other tables.
    • Triggers can use user-defined functions to activate non-database operations. This is useful, for example, for issuing alerts or updating information outside the database.
    • Note: An AFTER trigger can be created only on tables, not on views.

    How to Create After Triggers

    1. Working with INSERT Triggers

      INSERT INTO Customers
      VALUES (‘Mayank’,’Gupta’,’Hauz Khas’,’Delhi’,’Delhi’,’110016’,’01126853138’)
      INSERT INTO Customers
      VALUES(‘Himanshu’,’Khatri’,’ShahjahanMahal ’,’Jaipur’,’Rajesthan’,’326541’,’9412658745’)
      INSERT INTO Customers
      VALUES (‘Sarfaraz’,’Khan’,’Green Market’,’Hydrabad’,’AP’,’698542’,’9865478521’)

      INSERT INTO Products
      VALUES (‘ASP.Net Microsoft Press’,550)
      INSERT INTO Products
      VALUES (‘ASP.Net Wrox Publication’,435)
      INSERT INTO Products
      VALUES (‘ASP.Net Unleased’,320)
      INSERT INTO Products
      VALUES (‘ASP.Net aPress’,450)

      CREATE TRIGGER invUpdate ON [Orders]
      FOR INSERT
      AS
      UPDATE p SET p.instock=[p.instock – i.qty]
      FROM products p JOIN inserted I ON p.prodid = i.prodid

      You created an INSERT trigger that referenced the logical inserted table. Whenever you insert a new  record in the orders table now, the corresponding record in the products table will be updated to subtract the quantity of the order from the quantity on hand in the instack coloumn of the products table.

    2. Working with DELETE Triggers

      DELETE triggers are used for restricting the data that your users can remove from a database. For example

      CREATE TRIGGER DelhiDel ON [Customers]
      FOR DELETE
      AS
      IF (SELECT state FROM deleted) = ‘Delhi’
      BEGIN
      PRINT ‘Can not remove customers from Delhi’
      PRINT ‘Transaction has been canceled’
      ROOLBACK
      END


      DELETE trigger used the logical deleted table to make certain that you were not trying to delete a customer from the great state “Delhi” – if you did try to delete such a customer, you would be met with Mayank in the form of an error message (which was generated by the PRINT statement that you entered in the trigger code).

    3. Working with UPDATE Triggers

      UPDATE triggers are used to restrict UPDATE statement issued by your users, or to back your previous data.

      CREATE TRIGGER CheckStock ON [Products]
      FOR UPDATE
      AS
      IF (SELECT InStock FROM inserted) < 0
      BEGIN
      PRINT ‘Cannot oversell Products’
      PRINT ‘Transaction has been cancelled’
      ROLLBACK
      END

      You created an UPDATE trigger that references the inserted table to verify that you are not trying to insert a value that is less than zero. You need to check only the inserted table because SQL Server performs any necessary mathematical functions before inserting your data.

    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