A trigger is a block of code that constitutes a set of T-SQL statements that are activated in response to certain actions. A trigger can also be interpreted as a special kind of stored procedure that is executed whenever an action, such as data modification, takes place. and Where Triggers are Used

A trigger is always defined on a table, and is said to be fired whenever the data in the underlying table is affected by any of the Data Manipulation Language (DML) statements : INSERT, UPDATE or DELETE. A trigger fires in response to events like insertion, updating, and deletion of data.

Triggers help in maintaining consistent, reliable and correct data in tables. These enable complex actions to be performed, and cascade these actions to the other dependent tables. The characteristics of a trigger are as follows: It can be associated only with tables. it It cannot be defined on temporary tables or views. However, a trigger can reference temporary tables and views.

It It is fired automatically by SQL Server when any data modification statement is issued. It cannot be explicitly invoked or executed, as in the case of stored procedures. Triggers can be nested up to 32 levels. The nesting of triggers occurs when a trigger performs an action that initiates another trigger.
It prevents incorrect, unauthorized or inconsistent changes in data. It cannot return data to the user.
Triggers are most useful when the features supported by constraints cannot meet the functional needs of the application. For example:

FOREIGN KEY constraints do not support cascading activities, and they can validate a column value only with an exact match to a value in another column. If your application requires that a delete from one table will also result in a delete of related data in another table, or if the referential integrity requires something other than this, you must use a trigger. li A CHECK constraint can validate a column value only against a logical expression or another column in the same table. If your application requires that a column value be validated against a column in another table, you must use a trigger.
TYPES OF TRIGGERS
SQL Server supports the following types of triggers:

Insert Delete Update

INSERT Trigger

An INSERT trigger is fired whenever an attempt is made to insert a row in the trigger table. When an INSERT statement is issued, a new row is added to both, the trigger table and the inserted table. The inserted table is a logical table that holds a copy of the rows that that have been inserted into the trigger table.

DELETE Trigger

A DELETE trigger is fired whenever an attempt is made to delete rows from the trigger table. When a DELETE statement is issued, the rows from the trigger table are deleted and added to the deleted table. The deleted table and the trigger table do not have any rows in common, while in the case of an insert trigger, the inserted table and the trigger table have the inserted rows in common. Example
CREATE TRIGGER trg DeletePub -
ON Publishers
FOR DELETE
AS
DECLARE @RowsDeleted int
DECLARE @RowsLeft int
DECLARE @RowsLeft__char char (10)
DECLARE @Message char(30)
SELECT @RowsDeleted = (SELECT COUNT(*) FROM deleted)
SELECT @RowsLeft = {SELECT COUNT(*) FROM Publishers)
IF @RowsDeleted > 5
BEGIN
SELECT @RowsLeftChar = CONVERT (varchar(10), @RowsLeft)
SELECT @Message = * Only % + @RowsLeftChar -  are



Recent Tag : Website hosting | Domain | Developers blog | Dedicated | Reseller hosting | All types information | Software testing | Communication designing | Domain types | Career

directory-data All rights reserved