Sql triggers

20

Click here to load reader

Transcript of Sql triggers

Page 1: Sql triggers

Mail:- [email protected]://techgig.info

Page 2: Sql triggers

Mail:- [email protected]://techgig.info

WHO AM I ??

Page 3: Sql triggers

Mail:- [email protected]://techgig.info

I AM CHANDAN SOFTWARE DEVELOPERSINCE 1998

MCDBA & DBA

Page 4: Sql triggers

Mail:- [email protected]://techgig.info

What is a Trigger ?

A Trigger is a special kind of a store procedure that executes in response to certain action on the table like INSERTION, DELETION or UPDATION of data. It is a database object which is bound to a table and is executed automatically. You can’t explicitly invoke triggers. The only way to do this is by performing the required action on the table that they are assigned to.

Page 5: Sql triggers

Mail:- [email protected]://techgig.info

Definition of Trigger & basic difference between StoredProcedure & Trigger?

A trigger is a database object that is attached to a table. In many aspects it is similar to a stored procedure. As a matter of fact, triggers are often referred to as a "special kind of stored procedure." The main difference between a trigger and a stored procedure is that the former[Triger] is attached to a table and is only fired when an INSERT, UPDATE or DELETE occurs. You specify the modification action(s) that fire the trigger when it is created.

Page 6: Sql triggers

Mail:- [email protected]://techgig.info

The Name TRIGGER, itself signifies that something gets fired… and it may be before , after or instead of some event, i.e; an action in Database say - INSERT, UPDATE or DELETE

Types Of Triggers

There are three action query types that you use in SQL which are INSERT, UPDATE and DELETE. So, there are three types of triggers and hybrids that come from mixing and matching the events and timings that fire them. Basically, triggers are classified into two main types:

1. After Triggers (For Triggers)2. Instead Of Triggers

Page 7: Sql triggers

Mail:- [email protected]://techgig.info

• After TriggersThese triggers run after an INSERT, UPDATE or DELETE on a table. They are not supported for views. AFTER TRIGGERS can be classified further into three types as:•AFTER INSERT Trigger.•AFTER UPDATE Trigger.•AFTER DELETE Trigger.

Page 8: Sql triggers

Mail:- [email protected]://techgig.info

• Instead of TriggersThese can be used as an interceptor for anything that anyone tried to do on our table or view. If we define an Instead Of trigger on a table for the Delete operation, they try to delete rows, and they will not actually get deleted (unless we issue another delete instruction from within the trigger)INSTEAD OF TRIGGERS can be classified further into three types as:

•INSTEAD OF INSERT Trigger.•INSTEAD OF UPDATE Trigger.•INSTEAD OF DELETE Trigger.

Say, before Insert, Update or Delete in our coding we have put some validation and if one of the validation does not satisfy … so the delete or insert or update will not take place for which we have put the validation… and if we want to do something say roll back or throw message … instead of---- IT is utility of INSTEAD OF TRIGGER … means some action on behalf of main action [INSERT, UPDATE or DELETE]

Page 9: Sql triggers

Mail:- [email protected]://techgig.info

• When to use TriggersWhen we need to perform a certain action as a result of an INSERT, UPDATE or DELETE, we use TRIGGERS. We generally implement most of our data manipulation code via stored procedures and when we do this the trigger functionality can be moved into the procedure. For example, let's say we want to send an email to the Sales Manager when an order is entered whose priority is high. When SQL is used to insert the Orders row, a trigger is used to determine the OrderPriority and send the email when the criteria is met. The following shows a partial code listing of what this looks like.

Page 10: Sql triggers

Mail:- [email protected]://techgig.info

Page 11: Sql triggers

Mail:- [email protected]://techgig.info

• More example of Triggers…Say there are some list of Hospitals and we want to de-pannel some of them. As we run UPDATE statement for de-pannel we want to get the date of de-pannelment Triggered automatically as we run the UPDATE. So, it is an AFTER UPDATE Trigger

Page 12: Sql triggers

Mail:- [email protected]://techgig.info

Page 13: Sql triggers

Mail:- [email protected]://techgig.info

• How to find /View Triggers…Say there is some triggers and You know the name but do not know the inner Syntactical Details of the Trigger … what to do in short then to find out the detail ?

We can use the Sp_Help Trigger NameSP_help trg_GENGRVREF_NOThe out put is :-Name Owner Type Created_datetimetrg_GENGRVREF_NO dbo trigger 2014-02-16 02:31:21.833

But will not solve our purpose fully…..

Then what to do ??.....................

Page 14: Sql triggers

Mail:- [email protected]://techgig.info

Page 15: Sql triggers

Mail:- [email protected]://techgig.info

• Different Types of SQL Server TriggersIn Sql Server we can create four types of triggers Data Definition Language (DDL) triggers, Data Manipulation Language (DML) triggers, CLR triggers and Logon triggers.

Page 16: Sql triggers

Mail:- [email protected]://techgig.info

DDL Triggers

In SQL Server we can create triggers on DDL statements (like CREATE, ALTER, and DROP) and certain system defined stored procedures that perform DDL-like operations.Example : If you are going to execute the CREATE LOGIN statement or the sp_addlogin stored procedure to create login user, then both these can execute/fire a DDL trigger that you can create on CREATE_LOGIN event of Sql Server.We can use only FOR/AFTER clause in DDL triggers not INSTEAD OF clause means we can make only After Trigger on DDL statements.DDL trigger can be used to observe and control actions performed on the server, and to audit these operations. DDL triggers can be used to manage administrator tasks such as auditing and regulating database operations.

Page 17: Sql triggers

Mail:- [email protected]://techgig.info

DML TriggersIn SQL Server we can create triggers on DML statements (like INSERT, UPDATE, and DELETE) and stored procedures that perform DML-like operations. DML Triggers are of two typesAfter Trigger (using FOR/AFTER CLAUSE)This type of trigger fires after SQL Server finish the execution of the action successfully that fired it. Example : If you insert record/row in a table then the trigger related/associated with the insert event on this table will fire only after the row passes all the constraints, like as primary key constraint, and some rules. If the record/row insertion fails, SQL Server will not fire the After Trigger.Instead of Trigger (using INSTEAD OF CLAUSE)This type of trigger fires before SQL Server starts the execution of the action that fired it. This is differ from the AFTER trigger, which fires after the action that caused it to fire. We can have an INSTEAD OF insert/update/delete trigger on a table that successfully executed but does not include the actual insert/update/delete to the table.Example : If you insert record/row in a table then the trigger related/associated with the insert event on this table will fire before the row passes all the constraints, such as primary key constraint and some rules. If the record/row insertion fails, SQL Server will fire the Instead of Trigger.

Page 18: Sql triggers

Mail:- [email protected]://techgig.info

CLR TriggersCLR triggers are special type of triggers that based on the CLR (Common Language Runtime) in .net framework. CLR integration of triggers has been introduced with SQL Server 2008 and allows for triggers to be coded in one of .NET languages like C#, Visual Basic and F#.We coded the objects(like trigger) in the CLR that have heavy computations or need references to objects outside the SQL Server. We can write code for both DDL and DML triggers, using a supported CLR language like C#, Visual basic and F#. I will discuss CLR trigger later.

Page 19: Sql triggers

Mail:- [email protected]://techgig.info

LOGON TriggersLogon triggers are special type of trigger that fire when LOGON event of Sql Server is raised. This event is raised when a user session is being established with Sql Server that is made after the authentication phase finishes, but before the user session is actually established. Hence, all messages that we define in the trigger such as error messages, will be redirected to the SQL Server error log. Logon triggers do not fire if authentication fails. We can use these triggers to audit and control server sessions, such as to track login activity or limit the number of sessions for a specific login.

Page 20: Sql triggers

Mail:- [email protected]://techgig.info