Sie sind auf Seite 1von 3

Complied by John .

K & Bharat

TRIGGERS
1. What are triggers?
• Triggers are special type of Stored Procedures and execute
automatically (in place of or after data modifications) when
an Update, Insert or Delete statement is issued against a
table or view.
• Triggers can be used to automatically enforce business rules
when data is modified. The difference when compared to a
SP is that it can be activated when data is added or edited or
deleted from a table in a database.
• Triggers can be implemented to extend the integrity-
checking logic of constraints, defaults and rules.

2. How many triggers you can have on a table?


• In SQL Server 6.5 you could define only 3 triggers per table,
one for INSERT, one for UPDATE and one for DELETE.
• From SQL Server 7.0 onwards, this restriction is gone, and
you could create multiple triggers per each action. But in 7.0
there's no way to control the order in which the triggers are
fire.
• In SQL Server 2000 you could specify which trigger fires first
or fires last using sp_settriggerorder (In the After Trigger if
U have Multiple Triggers, U can specify which fires 1st & Last)

3. How to invoke a trigger on demand?


• Triggers can't be invoked on demand. They get triggered
only when an associated action (INSERT, UPDATE, DELETE)
happens on the table on which they are defined.

• Triggers are generally used to implement business rules,


auditing. Triggers can also be used to extend the referential
integrity checks, but wherever possible, use constraints for
this purpose, instead of triggers, as constraints are much
faster.

• Till SQL Server 7.0, triggers fire only after the data
modification operation happens. So in a way, they are called

Jan 2007
Complied by John .K & Bharat

post triggers. But in SQL Server 2000 you could create pre
triggers also. (INSTEAD OF triggers.)
• Also check out books online for 'inserted table', 'deleted
table' and COLUMNS_UPDATED()

There is a trigger defined for INSERT operations on a table,



in an OLTP system. The trigger is written to instantiate a
COM object and pass the newly insterted rows to it for some
custom processing. What do you think of this
implementation? Can this be implemented better?
Instantiating COM objects is a time consuming process and
since you are doing it from within a trigger, it slows down the
data insertion process. Same is the case with sending emails
from triggers. This scenario can be better implemented by
logging all the necessary data into a separate table, and
have a job which periodically checks this table and does the
needful.

4. Order of Action.
I. T-SQL Command
II. Inserted then Deleted
III. Instead of Trigger (Since SQL Server 2000)-Only 3 IT per
Table .
IV. Constraints
V. Data gets Inserted
VI. After Trigger - Multiple AT per Table.

5. Which virtual table does a trigger use?


Inserted and deleted table

6.Questions regarding RaiseError? How do U display user


defined error?
Learn more about RaiseError syntax and where they will be used.

7. Syntax
• CREATE TRIGGER - Creates a trigger, which is a special kind of stored
procedure that executes automatically when a user attempts the
specified data-modification statement on the specified table.

Jan 2007
Complied by John .K & Bharat

CREATE TRIGGER trigger_name


ON { table | view }
[ WITH ENCRYPTION ]
{
{ { FOR | AFTER | INSTEAD OF } { [ INSERT ] [ , ] [ UPDATE ] }
[ WITH APPEND ]
[ NOT FOR REPLICATION ]
AS
sql_statement [ ...n ]
}

• ALTER TRIGGER - Alters the definition of a trigger created previously by the


CREATE TRIGGER statement.

ALTER TRIGGER trigger_name


ON ( table | view )
[ WITH ENCRYPTION ]
{
{ ( FOR | AFTER | INSTEAD OF ) { [ DELETE ] [ , ] [ INSERT ] [ , ] [ UPDATE ] }
[ NOT FOR REPLICATION ]
AS
sql_statement [ ...n ]
}

• Drop Trigger – Removes one or more triggers from the current


database.
DROP TRIGGER { trigger name }

• Disable Trigger –disables a trigger


ALTER TABLE table name { ENABLE | DISABLE } TRIGGER { ALL |
trigger_name [ ,...n ] }

Jan 2007

Das könnte Ihnen auch gefallen