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

 

The cascade action is implemented by triggers that will examine the data and then either copy it to related tables or abort the operation.

 

Cascade Insert
 

Cascade insert is a figure of speech, there is no such thing. There is no cascading action when inserting a new row in [Tbl A] because there is no related data to be modified. However, the foreign key must only accept values present in [Tbl A].

Cascade insert requires one trigger for table [Tbl B] that will ensure that the data already exists in table [Tbl A], otherwise it will return an error.

 

CREATE TRIGGER "[Tbl B ITrig]" ON [Tbl B]  FOR INSERT AS

SET NOCOUNT ON

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

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

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

    BEGIN

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

        ROLLBACK TRANSACTION

    END

 

The code will compare the inserted data in the virtual table “inserted” with the data from table [Tbl A], if all data already exists then the number of rows from table “inserted” will be the same as then the number of rows from the intersection of table [Tbl A] and “inserted”. If not, the operation must be aborted and a system error flag is raised, an error message prepared and there will be a rollback to put an end to the transaction.

 

Inserting the same data as before:

 

INSERT [Tbl A] ([col A]) VALUES ('value1')

INSERT [Tbl A] ([col A]) VALUES ('value2')

INSERT [Tbl B] ([col B]) VALUES ('value1')

INSERT [Tbl B] ([col B]) VALUES ('value1')

INSERT [Tbl B] ([col B]) VALUES ('value2')

INSERT [Tbl B] ([col B]) VALUES ('value2')

INSERT [Tbl B] ([col B]) VALUES ('value3')

 

There is an error when trying to insert 'value3' which is fine.

Inserting a NULL value:

 

INSERT [Tbl B] ([col B]) VALUES (NULL)

 

The problem is that the trigger’s code is not ready to handle NULL values.
 

The solution is to add one more condition that verifies whether NULL values were inserted:

 

CREATE TRIGGER "[Tbl B ITrig]" ON [Tbl B]  FOR INSERT AS

SET NOCOUNT ON

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

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 added. Referential integrity rules require a related record in table ''[Tbl A]''.'

        ROLLBACK TRANSACTION

    END

 

OK, this solution works.


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