Sql triggers

991 views
812 views

Published on

SQL Triggers

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
991
On SlideShare
0
From Embeds
0
Number of Embeds
375
Actions
Shares
0
Downloads
22
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Sql triggers

  1. 1. Mail:- banerjee_chandan@rediffmail.com http://techgig.info
  2. 2. Mail:- banerjee_chandan@rediffmail.com http://techgig.info
  3. 3. Mail:- banerjee_chandan@rediffmail.com http://techgig.info
  4. 4. 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. Mail:- banerjee_chandan@rediffmail.com http://techgig.info
  5. 5. 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. Mail:- banerjee_chandan@rediffmail.com http://techgig.info
  6. 6. 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 Mail:- banerjee_chandan@rediffmail.com http://techgig.info
  7. 7. • After Triggers These 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. Mail:- banerjee_chandan@rediffmail.com http://techgig.info
  8. 8. • Instead of Triggers These 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] Mail:- banerjee_chandan@rediffmail.com http://techgig.info
  9. 9. • When to use Triggers When 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. Mail:- banerjee_chandan@rediffmail.com http://techgig.info
  10. 10. Mail:- banerjee_chandan@rediffmail.com http://techgig.info
  11. 11. • 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 Mail:- banerjee_chandan@rediffmail.com http://techgig.info
  12. 12. Mail:- banerjee_chandan@rediffmail.com http://techgig.info
  13. 13. • 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 Name SP_help trg_GENGRVREF_NO The out put is :Name Owner trg_GENGRVREF_NO Type dbo Created_datetime trigger 2014-02-16 02:31:21.833 But will not solve our purpose fully….. Then what to do ??..................... Mail:- banerjee_chandan@rediffmail.com http://techgig.info
  14. 14. Mail:- banerjee_chandan@rediffmail.com http://techgig.info
  15. 15. • Different Types of SQL Server Triggers In Sql Server we can create four types of triggers Data Definition Language (DDL) triggers, Data Manipulation Language (DML) triggers, CLR triggers and Logon triggers. Mail:- banerjee_chandan@rediffmail.com http://techgig.info
  16. 16. 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. Mail:- banerjee_chandan@rediffmail.com http://techgig.info
  17. 17. DML Triggers In 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 types After 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. Mail:- banerjee_chandan@rediffmail.com http://techgig.info
  18. 18. CLR Triggers CLR 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. Mail:- banerjee_chandan@rediffmail.com http://techgig.info
  19. 19. LOGON Triggers Logon 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. Mail:- banerjee_chandan@rediffmail.com http://techgig.info
  20. 20. Mail:- banerjee_chandan@rediffmail.com http://techgig.info

×