sql server ___________session_19(triggers)

12
Triggers

Upload: ehtisham-ali

Post on 10-Jan-2017

195 views

Category:

Education


2 download

TRANSCRIPT

Page 1: Sql server  ___________session_19(triggers)

Triggers

Page 2: Sql server  ___________session_19(triggers)

Microsoft SQL Server provides two primary mechanisms for enforcing business rules and data integrity: constraints and triggers. A trigger is a special type of stored procedure that automatically takes effect when a language event executes. SQL Server includes three general types of triggers: DML triggers, DDL triggers, and logon triggers.

Page 3: Sql server  ___________session_19(triggers)

DDL triggers are invoked when a data definition language (DDL) event takes place in the server or database.

DML triggers are invoked when a data manipulation language (DML) event takes place in the database. DML events include INSERT, UPDATE, or DELETE statements that modify data in a specified table or view. A DML trigger can query other tables and can include complex Transact-SQL statements. The trigger and the statement that fires it are treated as a single transaction, which can be rolled back from within the trigger. If a severe error is detected (for example, insufficient disk space), the entire transaction automatically rolls back.

Page 4: Sql server  ___________session_19(triggers)

DML triggers are useful in these ways:

They can cascade changes through related tables in the database; however, these changes can be executed more efficiently using cascading referential integrity constraints.

They can guard against malicious or incorrect INSERT, UPDATE, and DELETE operations and enforce other restrictions that are more complex than those defined with CHECK constraints.

Unlike CHECK constraints, DML triggers can reference columns in other tables. For example, a trigger can use a SELECT from another table to compare to the inserted or updated data and to perform additional actions, such as modify the data or display a user-defined error message.

They can evaluate the state of a table before and after a data modification and take actions based on that difference.

Multiple DML triggers of the same type (INSERT, UPDATE, or DELETE) on a table allow multiple, different actions to take place in response to the same modification statement.

Page 5: Sql server  ___________session_19(triggers)

AFTER Triggers AFTER triggers are executed after the action of the

INSERT, UPDATE, or DELETE statement is performed. Specifying AFTER is the same as specifying FOR, which is the only option available in earlier versions of Microsoft SQL Server. AFTER triggers can be specified only on tables.

INSTEAD OF Triggers INSTEAD OF triggers are executed in place of the

usual triggering action. INSTEAD OF triggers can also be defined on views with one or more base tables, where they can extend the types of updates a view can support.

Page 6: Sql server  ___________session_19(triggers)

Difference between Trigger and a Stored Procedure

TRIGGERS1. when you create a

trigger you have to identify event and action of your trigger.

2. trigger is run automatically if the event is occurred.

3. within a trigger you can call specific s.p.

STORED PROCEDURES

1. when you create s.p you don't identify event and action.

2. s.p don't run automatically but you have to run it manually.

3. within a s.p you cannot call a trigger.

Page 7: Sql server  ___________session_19(triggers)

example

CREATE TABLE Employee_Test(Emp_ID INT Identity,Emp_name Varchar(100),Emp_Sal Decimal (10,2))

INSERT INTO Employee_Test VALUES ('Anees',1000); INSERT INTO Employee_Test VALUES ('Rick',1200); INSERT INTO Employee_Test VALUES ('John',1100); INSERT INTO Employee_Test VALUES

('Stephen',1300); INSERT INTO Employee_Test VALUES ('Maria',1400);

Page 8: Sql server  ___________session_19(triggers)

CREATE TABLE Employee_Test_Audit ( Emp_ID int, Emp_name varchar(100), Emp_Sal decimal (10,2), Audit_Action varchar(100), Audit_Timestamp datetime )

Page 9: Sql server  ___________session_19(triggers)

AFTER INSERT Trigger CREATE TRIGGER trgAfterInsert ON Employee_Test FOR

INSERT AS declare @empid int; declare @empname varchar(100); declare @empsal decimal(10,2); declare @audit_action varchar(100); select @empid=i.Emp_ID from inserted i; select @empname=i.Emp_Name from inserted i; select @empsal=i.Emp_Sal from inserted i; set @audit_action='Inserted Record -- After Insert Trigger.'; insert into Employee_Test_Audit

(Emp_ID,Emp_Name,Emp_Sal,Audit_Action,Audit_Timestamp) values(@empid,@empname,@empsal,@audit_action,getdate()); PRINT 'AFTER INSERT trigger fired.‘

Page 10: Sql server  ___________session_19(triggers)

AFTER UPDATE Trigger CREATE TRIGGER trgAfterUpdate ON Employee_Test FOR UPDATE AS declare @empid int; declare @empname varchar(100); declare @empsal decimal(10,2); declare @audit_action varchar(100); select @empid=i.Emp_ID from inserted i; if update(Emp_Name) select @empname=i.Emp_Name from inserted i; set @audit_action='Updated Record -- After Update Trigger.'; if update(Emp_Sal) select @empsal=i.Emp_Sal from inserted i; set @audit_action='Updated Record -- After Update Trigger.'; insert into Employee_Test_Audit values(@empid,@empname,@empsal,@audit_action,getdate

()); PRINT 'AFTER UPDATE Trigger fired.'

Page 11: Sql server  ___________session_19(triggers)

AFTER DELETE Trigger CREATE TRIGGER trgAfterDelete ON Employee_Test AFTER

DELETE AS declare @empid int; declare @empname varchar(100); declare @empsal decimal(10,2); declare @audit_action varchar(100); select @empid=d.Emp_ID from deleted d; select @empname=d.Emp_Name from deleted d; select @empsal=d.Emp_Sal from deleted d; set @audit_action='Deleted -- After Delete Trigger.'; insert into Employee_Test_Audit

(Emp_ID,Emp_Name,Emp_Sal,Audit_Action,Audit_Timestamp) values(@empid,@empname,@empsal,@audit_action,getdate()); PRINT 'AFTER DELETE TRIGGER fired.'

Page 12: Sql server  ___________session_19(triggers)

Enable and disable trigger ALTER TABLE Employee_Test

{ENABLE|DISBALE} TRIGGER ALL

ALTER TABLE Employee_Test DISABLE TRIGGER trgAfterDelete