SlideShare a Scribd company logo
1 of 12
 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.
 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.
 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.
 AFTERTriggers
 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 OFTriggers
 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.
TRIGGERS
1. 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.
CREATETABLE Employee_Test
(
Emp_ID INT Identity,
Emp_nameVarchar(100),
Emp_Sal Decimal (10,2)
)
 INSERT INTO Employee_TestVALUES ('Anees',1000);
 INSERT INTO Employee_TestVALUES ('Rick',1200);
 INSERT INTO Employee_TestVALUES ('John',1100);
 INSERT INTO Employee_TestVALUES
('Stephen',1300);
 INSERT INTO Employee_TestVALUES ('Maria',1400);
 CREATETABLE Employee_Test_Audit
 (
 Emp_ID int,
 Emp_name varchar(100),
 Emp_Sal decimal (10,2),
 Audit_Action varchar(100),
 Audit_Timestamp datetime
 )
 CREATETRIGGER 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 InsertTrigger.';
 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.‘
 CREATETRIGGER 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 UpdateTrigger.';
 if update(Emp_Sal)
 select @empsal=i.Emp_Sal from inserted i;
 set @audit_action='Updated Record -- After UpdateTrigger.';
 insert into Employee_Test_Audit
 values(@empid,@empname,@empsal,@audit_action,getdate())
; PRINT 'AFTER UPDATETrigger fired.'
 CREATETRIGGER 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 DeleteTrigger.';
 insert into Employee_Test_Audit
(Emp_ID,Emp_Name,Emp_Sal,Audit_Action,Audit_Timestamp)
values(@empid,@empname,@empsal,@audit_action,getdate())
; PRINT 'AFTER DELETETRIGGER fired.'
 ALTERTABLE Employee_Test
{ENABLE|DISBALE}TRIGGER ALL
 ALTERTABLE Employee_Test DISABLE
TRIGGER trgAfterDelete

More Related Content

What's hot

Trigger and cursor program using sql
Trigger and cursor program using sqlTrigger and cursor program using sql
Trigger and cursor program using sql
Sushil Mishra
 

What's hot (18)

Oracle Database Trigger
Oracle Database TriggerOracle Database Trigger
Oracle Database Trigger
 
trigger dbms
trigger dbmstrigger dbms
trigger dbms
 
PL/SQL TRIGGERS
PL/SQL TRIGGERSPL/SQL TRIGGERS
PL/SQL TRIGGERS
 
Database Triggers
Database TriggersDatabase Triggers
Database Triggers
 
The Ultimate Guide to Oracle solaris 11 installation and configuration essent...
The Ultimate Guide to Oracle solaris 11 installation and configuration essent...The Ultimate Guide to Oracle solaris 11 installation and configuration essent...
The Ultimate Guide to Oracle solaris 11 installation and configuration essent...
 
SQL
SQLSQL
SQL
 
Oracle: Cursors
Oracle: CursorsOracle: Cursors
Oracle: Cursors
 
Introduction to triggers
Introduction to triggersIntroduction to triggers
Introduction to triggers
 
Trigger and cursor program using sql
Trigger and cursor program using sqlTrigger and cursor program using sql
Trigger and cursor program using sql
 
Triggers and active database
Triggers and active databaseTriggers and active database
Triggers and active database
 
The Ultimate Guide to Oracle web logic server 12c administration i 1z0 133
 The Ultimate Guide to Oracle web logic server 12c  administration i  1z0 133 The Ultimate Guide to Oracle web logic server 12c  administration i  1z0 133
The Ultimate Guide to Oracle web logic server 12c administration i 1z0 133
 
Sql tutorial
Sql tutorialSql tutorial
Sql tutorial
 
Cursors, triggers, procedures
Cursors, triggers, proceduresCursors, triggers, procedures
Cursors, triggers, procedures
 
MySql: Queries
MySql: QueriesMySql: Queries
MySql: Queries
 
Sql dml & tcl 2
Sql   dml & tcl 2Sql   dml & tcl 2
Sql dml & tcl 2
 
Procedures and triggers in SQL
Procedures and triggers in SQLProcedures and triggers in SQL
Procedures and triggers in SQL
 
The Ultimate Guide to Oracle solaris 11 advanced system administration 1 z0 822
The Ultimate Guide to Oracle solaris 11 advanced system administration  1 z0 822The Ultimate Guide to Oracle solaris 11 advanced system administration  1 z0 822
The Ultimate Guide to Oracle solaris 11 advanced system administration 1 z0 822
 
Les13
Les13Les13
Les13
 

Viewers also liked (12)

Sql create table statement
Sql create table statementSql create table statement
Sql create table statement
 
Sql wksht-7
Sql wksht-7Sql wksht-7
Sql wksht-7
 
Sql commands
Sql commandsSql commands
Sql commands
 
Part 15 triggerr
Part 15  triggerrPart 15  triggerr
Part 15 triggerr
 
Sql update statement
Sql update statementSql update statement
Sql update statement
 
Sql delete, truncate, drop statements
Sql delete, truncate, drop statementsSql delete, truncate, drop statements
Sql delete, truncate, drop statements
 
Introduction to-sql
Introduction to-sqlIntroduction to-sql
Introduction to-sql
 
Triggers-Sequences-SQL
Triggers-Sequences-SQLTriggers-Sequences-SQL
Triggers-Sequences-SQL
 
SQL Views
SQL ViewsSQL Views
SQL Views
 
SQL - Structured query language introduction
SQL - Structured query language introductionSQL - Structured query language introduction
SQL - Structured query language introduction
 
SQL Basics
SQL BasicsSQL Basics
SQL Basics
 
Sql ppt
Sql pptSql ppt
Sql ppt
 

Similar to Sql server ___________session_19(triggers)

Subqueries views stored procedures_triggers_transactions
Subqueries views stored procedures_triggers_transactionsSubqueries views stored procedures_triggers_transactions
Subqueries views stored procedures_triggers_transactions
maxpane
 
triggeroracle-eryk-130621201822-phpapp01.pdf
triggeroracle-eryk-130621201822-phpapp01.pdftriggeroracle-eryk-130621201822-phpapp01.pdf
triggeroracle-eryk-130621201822-phpapp01.pdf
saikumar580678
 

Similar to Sql server ___________session_19(triggers) (20)

Oracle - Program with PL/SQL - Lession 16
Oracle - Program with PL/SQL - Lession 16Oracle - Program with PL/SQL - Lession 16
Oracle - Program with PL/SQL - Lession 16
 
Oracle - Program with PL/SQL - Lession 17
Oracle - Program with PL/SQL - Lession 17Oracle - Program with PL/SQL - Lession 17
Oracle - Program with PL/SQL - Lession 17
 
triggers.pptx
triggers.pptxtriggers.pptx
triggers.pptx
 
Lab07_Triggers.pptx
Lab07_Triggers.pptxLab07_Triggers.pptx
Lab07_Triggers.pptx
 
Sql
SqlSql
Sql
 
Sql
SqlSql
Sql
 
Oracle sql material
Oracle sql materialOracle sql material
Oracle sql material
 
10 Creating Triggers
10 Creating Triggers10 Creating Triggers
10 Creating Triggers
 
Trigger
TriggerTrigger
Trigger
 
SQL interview questions jeetendra mandal - part 5
SQL interview questions jeetendra mandal - part 5SQL interview questions jeetendra mandal - part 5
SQL interview questions jeetendra mandal - part 5
 
Assignment#08
Assignment#08Assignment#08
Assignment#08
 
Les08
Les08Les08
Les08
 
Les08 (manipulating data)
Les08 (manipulating data)Les08 (manipulating data)
Les08 (manipulating data)
 
Subqueries views stored procedures_triggers_transactions
Subqueries views stored procedures_triggers_transactionsSubqueries views stored procedures_triggers_transactions
Subqueries views stored procedures_triggers_transactions
 
DOODB_LAB.pptx
DOODB_LAB.pptxDOODB_LAB.pptx
DOODB_LAB.pptx
 
T-SQL & Triggers
T-SQL & TriggersT-SQL & Triggers
T-SQL & Triggers
 
triggeroracle-eryk-130621201822-phpapp01.pdf
triggeroracle-eryk-130621201822-phpapp01.pdftriggeroracle-eryk-130621201822-phpapp01.pdf
triggeroracle-eryk-130621201822-phpapp01.pdf
 
Less09 Data
Less09 DataLess09 Data
Less09 Data
 
Les21
Les21Les21
Les21
 
4 trigger
4  trigger4  trigger
4 trigger
 

More from Ehtisham Ali

Sql server ___________session_20(ddl triggers)
Sql server  ___________session_20(ddl triggers)Sql server  ___________session_20(ddl triggers)
Sql server ___________session_20(ddl triggers)
Ehtisham Ali
 
Sql server ___________session3-normailzation
Sql server  ___________session3-normailzationSql server  ___________session3-normailzation
Sql server ___________session3-normailzation
Ehtisham Ali
 
Sql server ___________session2-data_modeling
Sql server  ___________session2-data_modelingSql server  ___________session2-data_modeling
Sql server ___________session2-data_modeling
Ehtisham Ali
 
Sql server ___________session_17(indexes)
Sql server  ___________session_17(indexes)Sql server  ___________session_17(indexes)
Sql server ___________session_17(indexes)
Ehtisham Ali
 
Sql server ___________session_16(views)
Sql server  ___________session_16(views)Sql server  ___________session_16(views)
Sql server ___________session_16(views)
Ehtisham Ali
 
Sql server ___________session_15(data integrity)
Sql server  ___________session_15(data integrity)Sql server  ___________session_15(data integrity)
Sql server ___________session_15(data integrity)
Ehtisham Ali
 
Sql server ___________session_11-12(joins)
Sql server  ___________session_11-12(joins)Sql server  ___________session_11-12(joins)
Sql server ___________session_11-12(joins)
Ehtisham Ali
 
Sql server ___________session_10(group by clause)
Sql server  ___________session_10(group by clause)Sql server  ___________session_10(group by clause)
Sql server ___________session_10(group by clause)
Ehtisham Ali
 

More from Ehtisham Ali (17)

Android tutorial
Android tutorialAndroid tutorial
Android tutorial
 
Sql server ___________session_20(ddl triggers)
Sql server  ___________session_20(ddl triggers)Sql server  ___________session_20(ddl triggers)
Sql server ___________session_20(ddl triggers)
 
Sql server ___________session3-normailzation
Sql server  ___________session3-normailzationSql server  ___________session3-normailzation
Sql server ___________session3-normailzation
 
Sql server ___________session2-data_modeling
Sql server  ___________session2-data_modelingSql server  ___________session2-data_modeling
Sql server ___________session2-data_modeling
 
Sql server ___________session_18(stored procedures)
Sql server  ___________session_18(stored procedures)Sql server  ___________session_18(stored procedures)
Sql server ___________session_18(stored procedures)
 
Sql server ___________session_17(indexes)
Sql server  ___________session_17(indexes)Sql server  ___________session_17(indexes)
Sql server ___________session_17(indexes)
 
Sql server ___________session_16(views)
Sql server  ___________session_16(views)Sql server  ___________session_16(views)
Sql server ___________session_16(views)
 
Sql server ___________session_15(data integrity)
Sql server  ___________session_15(data integrity)Sql server  ___________session_15(data integrity)
Sql server ___________session_15(data integrity)
 
Sql server ___________session_11-12(joins)
Sql server  ___________session_11-12(joins)Sql server  ___________session_11-12(joins)
Sql server ___________session_11-12(joins)
 
Sql server ___________session_10(group by clause)
Sql server  ___________session_10(group by clause)Sql server  ___________session_10(group by clause)
Sql server ___________session_10(group by clause)
 
Sql server ___________session_1-intro
Sql server  ___________session_1-introSql server  ___________session_1-intro
Sql server ___________session_1-intro
 
Sql server ___________session 3(sql 2008)
Sql server  ___________session 3(sql 2008)Sql server  ___________session 3(sql 2008)
Sql server ___________session 3(sql 2008)
 
Sql server ___________session 2(sql 2008)
Sql server  ___________session 2(sql 2008)Sql server  ___________session 2(sql 2008)
Sql server ___________session 2(sql 2008)
 
Sql server ___________session 1(sql 2008)
Sql server  ___________session 1(sql 2008)Sql server  ___________session 1(sql 2008)
Sql server ___________session 1(sql 2008)
 
Sql server ___________data type of sql server
Sql server  ___________data type of sql serverSql server  ___________data type of sql server
Sql server ___________data type of sql server
 
Sql server ___________data control language
Sql server  ___________data control languageSql server  ___________data control language
Sql server ___________data control language
 
Sql server ___________ (advance sql)
Sql server  ___________  (advance sql)Sql server  ___________  (advance sql)
Sql server ___________ (advance sql)
 

Recently uploaded

Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
ssuserdda66b
 

Recently uploaded (20)

SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 

Sql server ___________session_19(triggers)

  • 1.
  • 2.  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.
  • 3.  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.
  • 4.  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.
  • 5.  AFTERTriggers  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 OFTriggers  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.
  • 6. TRIGGERS 1. 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.
  • 7. CREATETABLE Employee_Test ( Emp_ID INT Identity, Emp_nameVarchar(100), Emp_Sal Decimal (10,2) )  INSERT INTO Employee_TestVALUES ('Anees',1000);  INSERT INTO Employee_TestVALUES ('Rick',1200);  INSERT INTO Employee_TestVALUES ('John',1100);  INSERT INTO Employee_TestVALUES ('Stephen',1300);  INSERT INTO Employee_TestVALUES ('Maria',1400);
  • 8.  CREATETABLE Employee_Test_Audit  (  Emp_ID int,  Emp_name varchar(100),  Emp_Sal decimal (10,2),  Audit_Action varchar(100),  Audit_Timestamp datetime  )
  • 9.  CREATETRIGGER 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 InsertTrigger.';  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.‘
  • 10.  CREATETRIGGER 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 UpdateTrigger.';  if update(Emp_Sal)  select @empsal=i.Emp_Sal from inserted i;  set @audit_action='Updated Record -- After UpdateTrigger.';  insert into Employee_Test_Audit  values(@empid,@empname,@empsal,@audit_action,getdate()) ; PRINT 'AFTER UPDATETrigger fired.'
  • 11.  CREATETRIGGER 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 DeleteTrigger.';  insert into Employee_Test_Audit (Emp_ID,Emp_Name,Emp_Sal,Audit_Action,Audit_Timestamp) values(@empid,@empname,@empsal,@audit_action,getdate()) ; PRINT 'AFTER DELETETRIGGER fired.'
  • 12.  ALTERTABLE Employee_Test {ENABLE|DISBALE}TRIGGER ALL  ALTERTABLE Employee_Test DISABLE TRIGGER trgAfterDelete