Sql Server

SQL | Triggers

SQL | Triggers

A trigger is a collection of SQL statements with particular names that are stored in system memory.

A trigger is a collection of SQL statements with particular names that are stored in system memory.
Triggers are special stored procedures that are executed automatically in response to the database object, database, and server events.
Every trigger has a table attached to it.


Difference between Triggers and Stored Procedures
1. A trigger is called automatically when a data modification event occurs against a table. A stored procedure, on the other hand, must be invoked directly.
2. Triggers have no chance of receiving parameters. we can pass as many parameters to a stored procedure.
3. A transaction cannot be committed or rolled back inside a trigger. We can committed or rolled back inside a stored procedure.


Common Syntax of all type of Triggers

CREATE TRIGGER [schema_name.]trigger_name
ON { table_name | view_name }
{ FOR | AFTER | INSTEAD OF } {[INSERT],[UPDATE],[DELETE]}
[NOT FOR REPLICATION]
AS
  {sql_statements}

/*
-- schema_name (optional) is the name of the schema where the new trigger will be created.
-- trigger_name is the name of the new trigger.
-- ON { table_name | view_name } keyword specifies the table or view name on which the trigger will be created.
-- AFTER clause specifies the INSERT, UPDATE or DELETE event which will fire the trigger.
The AFTER clause specifies that the trigger fires only after SQL Server successfully completes the execution of the action that fired it.
All other actions and constraints should be successfully executed before the trigger is fired.
-- INSTEAD OF clause is used to skip an INSERT, UPDATE or DELETE statement to a table and instead, executes other statements defined in the trigger.
So, the actual INSERT, UPDATE or DELETE statement does not happen at all. INSTEAD OF clause cannot be used on DDL triggers.
-- [NOT FOR REPLICATION] clause is specified to instruct the SQL Server not to invoke the trigger when a replication agent modifies the table.
-- sql_statements specifies the action to be executed when an event occurs.
*/


When we use triggers?
When we need to carry out specific actions automatically in particular desirable conditions, triggers will be useful.


Types of SQL Server Triggers
1.Data Definition Language (DDL) Triggers
2. Data Manipulation Language (DML) Triggers
3. Logon Triggers


DDL Triggers
DDL triggers are fired in response to the DDL events, such as CREATE, ALTER, and DROP statements.
We can create these triggers at the database level or server level, depending on the type of DDL events.
It can also be executed in response to certain system-defined stored procedures that do DDL-like operations.

The DDL triggers are useful in the following scenario:
When we need to prevent the database schema from changing
When we need to audit changes made in the database schema
When we need to respond to a change made in the database schema



DML Triggers
DML triggers are fired in response to DML events like INSERT, UPDATE, and DELETE statements in the table or view.
It can also be executed in response to DML-like operations performed by system-defined stored procedures.

The DML triggers can be classified into two types:
A. After Triggers
    After trigger fires, when SQL Server completes the triggering action successfully, that fired it.
    Generally, this trigger is executed when a table completes an insert, update or delete operations.
    It is not supported in views. Sometimes it is known as FOR triggers..
   Click Here to view More Detail on After/For Triggers.

B. Instead Of Triggers
   An INSTEAD OF trigger is a trigger that allows you to skip an INSERT, DELETE, or UPDATE 
   statement to a table or a view and execute other statements defined in the trigger instead.

  Click Here to view More Detail on Instead of Triggers.










Related Post

About Us

Community of IT Professionals

A Complete IT knowledgebase for any kind of Software Language, Development, Programming, Coding, Designing, Networking, Hardware and Digital Marketing.

Instagram