SlideShare a Scribd company logo
Sql server  ___________session_20(ddl triggers)
CREATETRIGGER [name of
trigger]
ON [scope (database|server)]
FOR [event]
As
--Trigger definition here…
 Create table back_up
(
actions varchar(20), action_date datetime
)
 create trigger ddl_trg on database
for create_table(,alter_table,drop_table)
as
declare @ac varchar(20);
SELECT top 1 @ac = name FROM sys.tables
insert into back_up values(@ac+' table has been created',
getdate())
print 'table created';
create table std
(
id int, name varchar(20)
)
select * from back_up
 create trigger ddl_db on all server
for create_database
as
declare @ac varchar(20);
declare @dt varchar(30);
SELECT top 100 percent @ac = name FROM sys.databases;
set @dt=getdate();
print @ac+' database has been created on '+ @dt;
create database student_management
 ALTERTABLE Employee_Test
{ENABLE|DISBALE}TRIGGER ALL
 ALTERTABLE Employee_Test DISABLE
TRIGGER trgAfterDelete
 Triggers are said to be nested if one trigger action initiates
another trigger.
 create trigger trgNestedInsert on Employee_Test_Audit for
insert
as
print 'NESTED INSERT trigger fired';
 These can be used as an interceptor for anything that
anyone tried to do on our table or view. If you 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 you issue another delete
instruction from within the trigger)
INSTEAD OFTRIGGERS can be classified further into
three types as:-
(a) INSTEAD OF INSERTTrigger.
(b) INSTEAD OF UPDATETrigger.
(c) INSTEAD OF DELETETrigger.
 CREATE TRIGGER trgInsteadOfDelete ON Employee_Test
 INSTEAD OF DELETE
 AS
 declare @emp_id int;
 declare @emp_name varchar(100);
 declare @emp_sal int;

 select @emp_id=d.Emp_ID from deleted d;
 select @emp_name=d.Emp_Name from deleted d;
 select @emp_sal=d.Emp_Sal from deleted d;
 BEGIN
 if(@emp_sal>1200)
 begin
 RAISERROR('Cannot delete where salary > 1200',16,1);
 ROLLBACK;
 end
 else
 begin
 delete from Employee_Test where Emp_ID=@emp_id;
 COMMIT;
 insert into
Employee_Test_Audit(Emp_ID,Emp_Name,Emp_Sal,Audit_Action,Audit_Timestamp)
 values(@emp_id,@emp_name,@emp_sal,'Deleted -- Instead Of
DeleteTrigger.',getdate());
 PRINT 'Record Deleted -- Instead Of DeleteTrigger.'
 end

More Related Content

DOCX
DOCX
A Shiny Example-- R
PPTX
CSharp v1.0.2
PDF
Bindings: the zen of montage
PDF
JDD2014: Real life lambdas - Peter Lawrey
PPTX
logistic regression with python and R
DOCX
Pratik Bakane C++
A Shiny Example-- R
CSharp v1.0.2
Bindings: the zen of montage
JDD2014: Real life lambdas - Peter Lawrey
logistic regression with python and R
Pratik Bakane C++

What's hot (20)

PPTX
support vector regression
PPTX
random forest regression
PDF
Mutation Testing - Voxxed Days Bucharest 10.03.2017
PPTX
5. Destructuring | ES6 | Assignment
PDF
Tweaking the interactive grid
PDF
Higher-Order Components — Ilya Gelman
DOCX
Algorithm for bisection method
PDF
Data Structure in C Programming Language
PDF
Linux/Rails学習用テキスト
PDF
Redux saga: managing your side effects. Also: generators in es6
PDF
C++ programs
PDF
ヘルスケアサービスを実現する最新技術 〜HealthKit・GCP + Goの活用〜
PPTX
กลุ่ม6
PPTX
simple linear regression
PDF
C programs Set 2
PPTX
The redux saga begins
PDF
Advanced Crystal Reports: Techniques for compiling Annual Reports & other sta...
PDF
Designing Immutability Data Flows in Ember
PDF
Bridging the Design to Development Gap with CSS Algorithms (Algorithms of CSS...
PPT
Oracle PLSql 4
support vector regression
random forest regression
Mutation Testing - Voxxed Days Bucharest 10.03.2017
5. Destructuring | ES6 | Assignment
Tweaking the interactive grid
Higher-Order Components — Ilya Gelman
Algorithm for bisection method
Data Structure in C Programming Language
Linux/Rails学習用テキスト
Redux saga: managing your side effects. Also: generators in es6
C++ programs
ヘルスケアサービスを実現する最新技術 〜HealthKit・GCP + Goの活用〜
กลุ่ม6
simple linear regression
C programs Set 2
The redux saga begins
Advanced Crystal Reports: Techniques for compiling Annual Reports & other sta...
Designing Immutability Data Flows in Ember
Bridging the Design to Development Gap with CSS Algorithms (Algorithms of CSS...
Oracle PLSql 4
Ad

Viewers also liked (7)

PPTX
Lead Trainer Report
DOCX
CV 2016
PPTX
One digital Good HQ presentation
DOC
Justificacion de nuestro proyecto
PPTX
mhlambi sobade
PDF
Showcasing PeopleLink Video enabled LMS at the World Education Summit 2016
PPT
S2 Verify
Lead Trainer Report
CV 2016
One digital Good HQ presentation
Justificacion de nuestro proyecto
mhlambi sobade
Showcasing PeopleLink Video enabled LMS at the World Education Summit 2016
S2 Verify
Ad

Similar to Sql server ___________session_20(ddl triggers) (20)

PPTX
Oracle trigger
PPTX
DOODB_LAB.pptx
PDF
Please fix the java code (using eclipse)package hw4p1;import jav.pdf
PDF
A Tour to MySQL Commands
DOCX
4.3 Hibernate example.docx
DOCX
INSERCION DE REGISTROS DESDE VISUAL.NET A UNA BD DE SQL SERVER
PPTX
DBMS UNIT 9.pptx..................................
PPT
PDF
Codigos
PDF
Oracle
DOCX
ggtimeseries-->ggplot2 extensions
PPT
Les09 (using ddl statements to create and manage tables)
PDF
User Defined Aggregation in Apache Spark: A Love Story
PDF
User Defined Aggregation in Apache Spark: A Love Story
PPT
PDF
groovy databases
PDF
1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
PDF
school-management-by-shivkamal-singh.pdf
PDF
pyspark_df.pdf
Oracle trigger
DOODB_LAB.pptx
Please fix the java code (using eclipse)package hw4p1;import jav.pdf
A Tour to MySQL Commands
4.3 Hibernate example.docx
INSERCION DE REGISTROS DESDE VISUAL.NET A UNA BD DE SQL SERVER
DBMS UNIT 9.pptx..................................
Codigos
Oracle
ggtimeseries-->ggplot2 extensions
Les09 (using ddl statements to create and manage tables)
User Defined Aggregation in Apache Spark: A Love Story
User Defined Aggregation in Apache Spark: A Love Story
groovy databases
1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
school-management-by-shivkamal-singh.pdf
pyspark_df.pdf

More from Ehtisham Ali (17)

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

Recently uploaded (20)

PDF
Pre independence Education in Inndia.pdf
PPTX
Lesson notes of climatology university.
PPTX
Institutional Correction lecture only . . .
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Basic Mud Logging Guide for educational purpose
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
01-Introduction-to-Information-Management.pdf
PPTX
Cell Types and Its function , kingdom of life
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
Pre independence Education in Inndia.pdf
Lesson notes of climatology university.
Institutional Correction lecture only . . .
Supply Chain Operations Speaking Notes -ICLT Program
Final Presentation General Medicine 03-08-2024.pptx
Pharmacology of Heart Failure /Pharmacotherapy of CHF
STATICS OF THE RIGID BODIES Hibbelers.pdf
Basic Mud Logging Guide for educational purpose
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
human mycosis Human fungal infections are called human mycosis..pptx
O7-L3 Supply Chain Operations - ICLT Program
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
01-Introduction-to-Information-Management.pdf
Cell Types and Its function , kingdom of life
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
O5-L3 Freight Transport Ops (International) V1.pdf
Microbial diseases, their pathogenesis and prophylaxis
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Module 4: Burden of Disease Tutorial Slides S2 2025

Sql server ___________session_20(ddl triggers)

  • 2. CREATETRIGGER [name of trigger] ON [scope (database|server)] FOR [event] As --Trigger definition here…
  • 3.  Create table back_up ( actions varchar(20), action_date datetime )  create trigger ddl_trg on database for create_table(,alter_table,drop_table) as declare @ac varchar(20); SELECT top 1 @ac = name FROM sys.tables insert into back_up values(@ac+' table has been created', getdate()) print 'table created';
  • 4. create table std ( id int, name varchar(20) ) select * from back_up
  • 5.  create trigger ddl_db on all server for create_database as declare @ac varchar(20); declare @dt varchar(30); SELECT top 100 percent @ac = name FROM sys.databases; set @dt=getdate(); print @ac+' database has been created on '+ @dt;
  • 7.  ALTERTABLE Employee_Test {ENABLE|DISBALE}TRIGGER ALL  ALTERTABLE Employee_Test DISABLE TRIGGER trgAfterDelete
  • 8.  Triggers are said to be nested if one trigger action initiates another trigger.  create trigger trgNestedInsert on Employee_Test_Audit for insert as print 'NESTED INSERT trigger fired';
  • 9.  These can be used as an interceptor for anything that anyone tried to do on our table or view. If you 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 you issue another delete instruction from within the trigger) INSTEAD OFTRIGGERS can be classified further into three types as:- (a) INSTEAD OF INSERTTrigger. (b) INSTEAD OF UPDATETrigger. (c) INSTEAD OF DELETETrigger.
  • 10.  CREATE TRIGGER trgInsteadOfDelete ON Employee_Test  INSTEAD OF DELETE  AS  declare @emp_id int;  declare @emp_name varchar(100);  declare @emp_sal int;   select @emp_id=d.Emp_ID from deleted d;  select @emp_name=d.Emp_Name from deleted d;  select @emp_sal=d.Emp_Sal from deleted d;  BEGIN  if(@emp_sal>1200)  begin  RAISERROR('Cannot delete where salary > 1200',16,1);  ROLLBACK;  end  else  begin  delete from Employee_Test where Emp_ID=@emp_id;  COMMIT;  insert into Employee_Test_Audit(Emp_ID,Emp_Name,Emp_Sal,Audit_Action,Audit_Timestamp)  values(@emp_id,@emp_name,@emp_sal,'Deleted -- Instead Of DeleteTrigger.',getdate());  PRINT 'Record Deleted -- Instead Of DeleteTrigger.'  end