-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrigger.sql
75 lines (63 loc) · 1.74 KB
/
Trigger.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
create database Trigger1;
use Trigger1;
create table Sports
(
Id int Primary Key,
Spname Varchar(45),
Fees Int,
Gender Char(1),
Studname Varchar(12)
)
insert into Sports values(1,'Cricket',1500,'F','Kiran Joshi'),
(2,'Cricket',1500,'F','Mitali Rao'),
(3,'FootBall',1200,'M','Sameer Jha'),
(4,'FootBall',1200,'F','Sneha Soni');
Select * from Sports;
create table Stud_Audit_Test
(
Id int Identity,
Audit_Action text
);
Go
/* ---------------------------Insert---------------------------------- */
create trigger trInsertstud
On sports
for insert
as
begin
declare @Id int
Select @Id =Id from inserted
insert into Stud_Audit_Test
values ('New Student with Id = ' + Cast(@Id As Varchar(10)) + ' is added at '+ cast(Getdate() as varchar(22)))
end
insert into Sports values(6,'tennis',2500,'M','Mehul Sharma');
select * from Stud_audit_Test;
go
/* ----------------------------- Delete ---------------------------------- */
Create trigger trDeletestud
on Sports
for Delete
As
Begin
Declare @Id int
Select @Id = Id from deleted
Insert into Stud_Audit_Test
values ('An existing employee with Id =' + cast (@Id as varchar(10)) +' is deleted at ' + cast (getdate() as varchar(22)))
end
delete from Sports where id=5;
select * from Stud_Audit_Test;
go
/* --------------------------------------------- Update --------------------------------- */
create Trigger trUpdatestud
On Sports
for Update
as
begin
Declare @Id int
select @Id = Id from inserted
Insert Into Stud_Audit_Test
values ('An existing employee with id = ' + cast(@Id As varchar(10)) + ' is Updated at ' + cast (Getdate() as varchar(22)))
end
select * from Sports;
update Sports set gender = 'M' where id=4;
select * from Stud_Audit_Test;