SQL 15
SQL 15
--Trigger
--Triggers fired automatically , Once we perform any DML (INSERT,DELETE and UPDATE)
opeartion on table.
--Triggers are also known as special kind of store procedure.
--syntax:
--create trigger tr_Trigger_Name
--on TBALE_NAME
--for INSERT/DELETE/UPDATE
--as
--begin
--END
--Insert trigger
create trigger tr_TEST_INSERT
on employee
for insert
as
begin
end
--NOTE:
--INSERTED/DELETED table is special kind of table which is available within the
context of trigger.
--select * from inserted/deleted
--1.It returns exactly the same copy or replica on which table we are creating a
trigger.
--2.Structure of inserted/deleted table within the context of trigger is identical
or same on which table trigger is created.
--After creating insert trigger if we insert data then it will execute the same SQL
statement which is present within the context of trigger.
insert into emp values (7,'Damini','Mumbai')
--DELETE trigger
create trigger tr_TEST_DELETE
on EMPLOYEE
for delete
as
begin
select * from deleted
end
--Update trigger
--update = delete then INSERT
--By using trigger we will store information in table about insert,delete and
update.
create table AuditInfo(ID int identity , AuditInfo Varchar(300))
insert into AuditInfo values (CAST(@eid as varchar(4)) + ' '+ 'is INSERTED at ' + '
'+
cast(GETDATE() as varchar(20)))
END
select * from EMPLOYEE
select * from AuditInfo
insert into EMPLOYEE values (5,'Rphan',11000,'2020-09-02','411027')
insert into AuditInfo values (@eid + ' '+ 'is DELETED at ' + ' '+ cast(GETDATE() as
varchar(20)))
END
insert into AuditInfo values (@eid + ' '+ 'is Updated at ' + ' '+ cast(GETDATE() as
varchar(20)))
END