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

 

No action

 

This cascade action is implemented by triggers that will abort the operation if related data is found.

 

Update No Action

 

Update No Action requires one trigger for table [Tbl A] that will abort the operation if there are related columns in table [Tbl B].

 

CREATE TRIGGER "[Tbl A Utrig]" ON [Tbl A] FOR UPDATE AS

SET NOCOUNT ON

/* * PREVENT UPDATES IF DEPENDENT RECORDS IN [Tbl B]*/

IF UPDATE([Col A])

    BEGIN

IF (SELECT COUNT(*) FROM deleted, [Tbl B] WHERE (deleted. [Col A]= [Tbl B]. [Col B])) > 0

    BEGIN

        RAISERROR 44445 'The record can''t be changed. Since related records exist in table ''[Tbl B]'', referential integrity rules would be violated.'

        ROLLBACK TRANSACTION

    END

END

 

The code will first check if column [Col A] was updated, in this case there is only one column being updated but there are other cases when several columns are updated and it might be useful to know which. Next it will check if there are related columns in table [Tbl B], if so, it will return an error message and abort the operation by rolling back the transaction. Otherwise, nothing will happen.

 

Testing the trigger:

 

UPDATE [Tbl A] SET [col A]='2222' WHERE [col A]='value2'

 

By checking the contents of table [Tbl A] we can verify that the column was updated 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