-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEducation Triggers.sql
More file actions
176 lines (154 loc) · 5.61 KB
/
Education Triggers.sql
File metadata and controls
176 lines (154 loc) · 5.61 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
create trigger trgLogPerson
ON Education.Person
after update, delete
as begin
-- update
insert into Education.Log(userType, userId, action)
select i.role, i.ID, 'update Person'
from inserted i
join deleted d on i.ID = d.ID
where exists(
select i.Id, i.role, i.majorID, i.isActive
except
select d.Id, d.role, d.majorID, d.isActive
);
-- delete
insert into Education.Log(userType, userId, action)
select role, ID, 'delete from Person'
from deleted
EXCEPT
select role, ID, 'delete from Person'
from inserted;
end;
GO
create trigger trgLogCourse
ON Education.Course
after insert, update, delete
as begin
-- insert
insert into Education.Log(userType, userId, action)
select 'system', NULL, concat('insert into Course: ', title)
from inserted
EXCEPT
select 'system', NULL, concat('insert into Course: ', title)
from deleted;
-- update
insert into Education.Log(userType, userId, action)
select 'system', NULL, concat('update Course ID=', i.courseID, ', title changed to ', i.title)
from inserted i
join deleted d on i.courseID = d.courseID
where exists(
select i.courseID, i.title, i.credit, i.courseMajorID
EXCEPT
select d.courseID, d.title, d.credit, d.courseMajorID
);
-- delete
insert into Education.Log(userType, userId, action)
select 'system', NULL, concat('delete from Course: ', title)
from deleted
EXCEPT
select 'system', NULL, concat('delete from Course: ', title)
from inserted;
end;
GO
create trigger trgLogSection
ON Education.Section
after insert, update, delete
as begin
-- insert
insert into Education.Log(userType, userId, action)
select 'system', instructorID, concat('insert into Section: courseID=', courseID, ', year=', year, ', semester=', semester)
from inserted
EXCEPT
select 'system', instructorID, concat('insert into Section: courseID=', courseID, ', year=', year, ', semester=', semester)
from deleted;
-- update
insert into Education.Log(userType, userId, action)
select 'system', i.instructorID, concat('update Section secID=', i.secID, ', courseID=', i.courseID)
from inserted i
join deleted d on i.secID = d.secID and i.courseID = d.courseID
where EXISTS(select i.* EXCEPT select d.*);
-- delete
insert into Education.Log(userType, userId, action)
select 'system', instructorID, concat('delete from Section: courseID=', courseID, ', year=', year, ', semester=', semester)
from deleted
EXCEPT
select 'system', instructorID, concat('delete from Section: courseID=', courseID, ', year=', year, ', semester=', semester)
from inserted;
end;
GO
create trigger trgAfterAddPerson
ON Education.Person
after insert
as begin
if exists(select 1 from inserted where Education.validateNationalCode(nationalCode) = 0)
begin
-- if any inserted row has an invalid national code:
Print('One or more national codes are invalid. The operation has been cancelled.');
ROLLBACK TRANSACTION;
return;
end
update p
set LibraryAccountID = Education.createLibraryID(p.entryDate, p.majorID, p.ID)
from Education.Person p join inserted i on p.ID = i.ID;
-- insert
insert into Education.Log(userType, userId, action)
select role, ID, 'inserted into Person'
from inserted;
-- except
-- select role, ID, 'inserted into Person'
-- from deleted; --- deleted in(after insert) is always empty.
insert into Education.Student(studentID)
select i.ID from inserted i where i.role = 'student';
insert into Education.Instructor(instructorID)
select i.ID from inserted i where i.role = 'instructor';
insert into Library.Account(accountID, personID)
select Education.createLibraryID(i.entryDate, i.majorID, i.ID), i.ID
from inserted i
where i.role IN('student', 'instructor');
insert into Library.Librarian(librarianID)
select i.ID
from inserted i
where i.role = 'librarian' and i.majorID = 5;
end;
GO
create trigger trgDeactivatePerson
on Education.Person after update
as begin
update a
set a.isActive = 0
from Library.Account a join deleted d on d.ID = a.personID join inserted i on a.personID = i.ID
where d.isActive = 0 and i.isActive = 1;
end;
GO
create trigger Education.checkGraduation
on Education.Takes after update
as begin
update p
set isActive = 0
from Education.Person p
where p.role = 'student' and p.isActive = 1
and exists(select 1 from inserted i where i.studentID = p.ID)
and (select sum(c.credit) from Education.Takes t
join Education.Course c on t.courseID = c.courseID
where t.studentID = p.ID and t.status = 'passed') >= 75
and not exists(select 1 from Education.Takes t2 where t2.studentID = p.ID and t2.status = 'ongoing');
declare @graduated table(studentID int);
insert into @graduated(studentID)
select p.ID
from Education.Person p
where p.role = 'student' and p.isActive = 1
and exists(select 1 from inserted i where i.studentID = p.ID)
and (select sum(c.credit) from Education.Takes t
join Education.Course c on t.courseID = c.courseID
where t.studentID = p.ID and t.status = 'passed') >= 75
and not exists(select 1 from Education.Takes t2 where t2.studentID = p.ID and t2.status = 'ongoing');
declare @stuID int;
while exists(select 1 from @graduated)
begin
select top 1 @stuID = studentID from @graduated;
print('graduated: ' + cast(@stuID as varchar));
delete from @graduated where studentID = @stuID;
end
end;
go