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

 

Triggers

 

Triggers are objects bound to tables; they contain TSQL statements and will execute for each row modified. INSERT, UPDATE, or DELETE statements will cause a trigger to execute.

 

There are two types of triggers in SQL Server: AFTER and INSTEAD OF.

 

AFTER triggers
 

This type of trigger will execute after the operation that set it off is completed. The trigger will only execute after all the referential cascade actions and constraint checks reached completion.
 

AFTER triggers are the type used by previous versions of SQL Server while INSTEAD OF appeared with SQL Server 2000. This is something to be considered when updating a database and reusing code.

 

Example: create a trigger that will accept inserting data only on the first five days of the month, in table jobs from database Pubs.

 

CREATE TRIGGER "jobs_ITrig" ON jobs FOR INSERT AS

SET NOCOUNT ON

/* * Prevent INSERT if not within the first 5 days of the month */

IF (day(getdate())>5)

    BEGIN

        RAISERROR 44447 'The record can''t be added. Insertion is restricted to first 5 days of the week.'

        ROLLBACK TRANSACTION

    END

 

Trying to add a new record (assuming a day of the month above five):

 

INSERT jobs(job_desc, min_lvl, max_lvl) VALUES( 'Trainee', 11, 12)

 

Will result on an error:

 

Server: Msg 44447, Level 16, State 1, Procedure jobs_ITrig, Line 6

The record can't be added. Insertion is restricted to first 5 days of the week.
 


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