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

Using Triggers
By: McGraw-Hill/Osborne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 5
    2006-09-28

    Table of Contents:
  • Using Triggers
  • Cascading Deletes
  • Aggregates
  • Enforce Schema Integrity Among Objects on Different Servers or Databases

  • 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 - Cascading Deletes


    (Page 2 of 4 )

    Usually, referential integrity between two tables is implemented with a foreign key, such as in the following illustration:

    In such cases, a foreign key prevents the user from deleting records from a parent table (Inventory) if a record is referenced in a linked table (InventoryProperty). The only way to delete the record would be to use the following code:

    Delete dbo.InventoryProperty
    Where InventoryId = 222

    Delete dbo.Inventory
    Where InventoryId = 222

    In some cases, the system design requirements might call for cascading deletes, which automatically delete records from the linked table when the record in the parent table is deleted. In this case, only one command is required to delete any instance of an asset with an InventoryId value of 222:

    Delete dbo.Inventory
    Where InventoryId = 222

    SQL Server 2000 introduced cascading referential integrity constraints that can implement this behavior. In SQL Server 7.0 and earlier versions, you had to use triggers to implement cascading operations. It is now recommended to use cascading referential integrity constraints, but since that is not possible in some cases (for example, when tables are in different databases), you should know how to do it with triggers.

    The following example creates two new tables (without a foreign key), populates them with a few records, and creates a trigger that will implement a cascading delete:

    Create Table dbo.MyInventory
         (
         Inventoryid int Not Null Identity (1, 1),
         EqId int Null,
         LocationId int Null,
         StatusId tinyint Null,
         LeaseId int Null,
         LeaseScheduleId int Null,
         OwnerId int Null,
         Rent smallmoney Null,
         Lease smallmoney Null,
         Cost smallmoney Null, 
         AcquisitionTypeID tinyint Null
         )
    Go

    Alter Table dbo.MyInventory Add Constraint
         PK_Inventory Primary Key Nonclustered
         (
         Inventoryid
         )
    Go

    Create Table dbo.MyInventoryProperty
         (
         InventoryId int Not Null,
         PropertyId smallint Not Null,
         Value varchar(50) Null
         )
    Go
    Alter Table dbo.MyInventoryProperty Add Constraint
         PK_InventoryProperty Primary Key Nonclustered
         (
         InventoryId,
         PropertyId
         )
    Go
    Create Trigger dbo.trMyInventory_CascadingDelete_D
    On dbo.MyInventory
    After Delete   --For delete
    As

    If @@Rowcount = 0
         Return
    Delete dbo.MyInventoryProperty
    where InventoryId In (Select InventoryID from deleted)
    Go

    Insert into dbo.MyInventory(EqId) Values (1)
    Insert into dbo.MyInventory(EqId) Values (2)
    Insert into dbo.myInventory(EqId) Values (3)
    Insert into dbo.myInventory(EqId) Values (4)
    Insert into dbo.myInventory(EqId) Values (5)

    Insert into MyInventoryProperty(InventoryId, PropertyId, Value)
    Values (1, 1, 'ACME')
    Insert into MyInventoryProperty(InventoryId, PropertyId, Value)
    Values (1, 2, 'Turbo')
    Insert into MyInventoryProperty(InventoryId, PropertyId, Value)
    Values (1, 3, '311')
    Insert into MyInventoryProperty(InventoryId, PropertyId, Value)
    Values (2, 1, 'ACME')
    Insert into MyInventoryProperty(InventoryId, PropertyId, Value)
    Values (2, 2, 'TurboPro')
    Insert into MyInventoryProperty(InventoryId, PropertyId, Value)
    Values (2, 3, '312')
    Go

    Delete MyInventory
    Where InventoryId = 1

    Select * from MyInventory
    Select * from MyInventoryProperty

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