You are Here: FAQ ->Scripting & Programming->MSSQL->Article #7


WebHosting Microsoft-Edition This Article is for 1&1 Microsoft Web Hosting Only.


Triggers



Triggers are result driven procedures which are being triggered when a predefined action (e. g. an update of a certain table) is executed.

They can be created as follows:

CREATE TRIGGER triggername ON targettable FOR INSERT
AS
INSERT INTO table (column1) VALUES('value1')


This way a trigger called "triggername" will be created, which will run the SQL command "insert into table (column1) values('value')" (which will perform an INSERT on a different table) any time an INSERT is performed on the "targettable" table.

A trigger can be removed with

DROP TRIGGER triggername


A trigger can be modified with

ALTER TRIGGER triggername ON targettable FOR INSERT

AS

INSERT INTO table (column1) VALUES('value1')


A listing of all triggers active in the current database (including the corresponding SQL) queries can be performed with the following query:

SELECT name, text FROM sysobjects LEFT JOIN syscomments ON sysobjects.id = syscomments.id WHERE type = 'tr'


Print Article
How useful was this article?
(From 5 = Very Useful to 1 = Not Very Useful at all):
1 2 3 4 5