Call now: (800) 766-1884  



 Home


 SQL Server Tips
 SQL Server Training

 SQL Server Consulting
 SQL Server Support
 SQL Server Remote DBA



 Articles
 Services
 SQL Server Scripts
 Scripts Menu



 

 

 

 
  SQL Server Tips by Gama and Naughter

 

Cascade Update

 

Cascade update requires two triggers: one for table [Tbl A] that will update the related columns from table [Tbl B] and one for table [Tbl B] that will ensure that updated data will have a value that already exists in table [Tbl A], otherwise it will return an error.

 

For table [Tbl A]:

CREATE TRIGGER "[ Tbl A UTrig]" ON [Tbl A] FOR UPDATE AS
SET NOCOUNT ON
/* * CASCADE UPDATES TO '[Tbl B]' */
IF UPDATE([Col A])
    BEGIN
       UPDATE [Tbl B]
       SET [Tbl B].[Col B]=inserted.[Col A]
       FROM [Tbl B], deleted, inserted
       WHERE deleted.[Col A]= [Tbl B].[Col B]
         AND deleted.[Col A]= inserted.[Col A]
    END

 

 

The code will check if column [Col A] was updated with the function UPDATE(), next it will update table [Col B] in table [Tbl B] with the new values for [Col A] in table [Tbl A]. The filtering will require the old values for [Col A] in table [Tbl A] because the transaction has not been committed yet. This sounds confusing but it is easier to understand by remembering that the virtual table inserted will contain the new values for the updated data and the virtual table deleted will contain the old ones.

 

Let us try the trigger:

 

UPDATE [Tbl A] SET [col A]='1111' WHERE [col A]='value1'

 

For table [Tbl B]:

 

CREATE TRIGGER "[Tbl B UTrig]" ON [Tbl B]  FOR UPDATE AS

SET NOCOUNT ON

/* * PREVENT UPDATES IF NO MATCHING KEY IN '[Tbl A]' */

IF UPDATE([Col B])

    BEGIN

IF (SELECT COUNT(*) FROM inserted) !=

   (SELECT COUNT(*) FROM [Tbl A], inserted WHERE ([Tbl A].[Col A] = inserted.[Col B]))

AND

 (SELECT COUNT([Col B]) FROM inserted) !=

   (SELECT COUNT([Col B]) FROM inserted WHERE (inserted.[Col B] IS NULL))

    BEGIN

        RAISERROR 44447 'The record can''t be changed. Referential integrity rules require a related record in table ''[Tbl A]''.'

        ROLLBACK TRANSACTION

    END

END

 

Let us insert two NULL rows in table [Tbl B] and then try to update it:

 

UPDATE [Tbl B] SET [col B]='2222' WHERE [col B] IS NULL

 

This will generate an error because there are no related values in table [Tbl A], which is correct.

 

UPDATE [Tbl B] SET [col B]='1111' WHERE [col B] IS NULL

 

This will update the rows properly.


The above book excerpt is from:

Super SQL Server Systems
Turbocharge Database Performance with C++ External Procedures

ISBN: 0-9761573-2-2
Joseph Gama, P. J. Naughter

 http://www.rampant-books.com/book_2005_2_sql_server_external_procedures.htm
 

 

Burleson Consulting Remote DB Administration


 

 


 

 

 

 

 
Burleson is the America's Team

Note: The pages on this site were created as a support and training reference for use by our staff of DBA consultants.  If you find it confusing, please exit this page.

Errata?  SQL Server technology is changing and we strive to update our SQL Server support information.  If you find an error or have a suggestion for improving our content, we would appreciate your feedback.  Just  e-mail:and include the URL for the page.
 


Burleson Consulting
SQL Server database support

 

Copyright 1996 -  2013 by Vaaltech Web Services. All rights reserved.

Hit Counter