-
Notifications
You must be signed in to change notification settings - Fork 1
/
Triggers.sql
209 lines (176 loc) · 6.66 KB
/
Triggers.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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
create or alter trigger ConferenceCancellation
on Conference
for update
as
begin
if update(Is_Cancelled)
begin
if ((select Is_Cancelled from inserted) = 1)
begin
create table #daysToCancel(
dayID int
)
insert into #daysToCancel
select Conference_Day_ID
from Conference_Day
where Conference_Day.Conference_ID = (select Conference_ID from inserted)
update Conference_Day
set Is_Cancelled = 1
where Conference_Day.Conference_Day_ID in (select * from #daysToCancel)
drop table #daysToCancel
end
end
end
create or alter trigger ConferenceDayCancellation
on Conference_Day
for update
as
begin
if update(Is_Cancelled)
begin
create table #reservationsToCancel(
reservationID int
)
insert into #reservationsToCancel
select Reservation_ID
from Reservation
where Reservation.Conference_Day_ID in (select Conference_Day_ID
from inserted
where inserted.Is_Cancelled = 1)
update Reservation
set Is_Cancelled = 1
where Reservation_ID in (select * from #reservationsToCancel)
drop table #reservationsToCancel
end
end
create or alter trigger ReservationCancellation
on Reservation
after update
as
begin
if update(Is_Cancelled)
begin
delete from Conference_Day_Participant --remove all people with this reservation from participants list
where Reservation_ID in (select Reservation_ID from inserted)
create table #workshopReservationsToCancel(
workshopReservationID int
)
insert into #workshopReservationsToCancel
select Workshop_Reservation_ID
from Workshop_reservation
where Workshop_reservation.Reservation_ID in (select Reservation_ID
from inserted
where inserted.Is_Cancelled = 1)
update Workshop_reservation
set Is_Cancelled = 1
where Workshop_Reservation_ID in (select * from #workshopReservationsToCancel)
drop table #workshopReservationsToCancel
end
end
create or alter trigger WorkshopReservationCancellation
on Workshop_reservation
after update
as
begin
if update(Is_Cancelled)
begin
delete from Workshop_Participant --remove all people with this reservation from participants list
where Workshop_Reservation_ID in (select Workshop_Reservation_ID
from inserted
where inserted.Is_Cancelled = 1)
end
end
create or alter trigger DayConferenceParticipantsLimitChange
on Conference_Day
for update
as
begin
if update(Participants_Limit)
begin
if (select Participants_Limit from inserted)
<
ISNULL((select sum(Normal_Ticket_Count + Student_Ticket_Count)
from inserted
inner join Reservation
ON Reservation.Conference_Day_ID = inserted.Conference_Day_ID
), 0)
begin
update Conference_Day set Participants_Limit = deleted.Participants_Limit
from dbo.Conference_Day
inner join deleted
on Conference_Day.Conference_Day_ID = deleted.Conference_Day_ID
raiserror ('You cannot set participants limit to value smaller than current reservations', 16, 1);
rollback transaction;
return;
end
end
end
end
create or alter trigger WorkshopConferenceParticipantsLimitChange
on Workshop_In_Day
for update
as
begin
if update(Participants_Limit)
begin
if (select Participants_Limit from inserted)
<
ISNULL((select sum(Workshop_Reservation.Ticket_Count)
from inserted
inner join Workshop_reservation
on Workshop_reservation.Conference_Day_ID = inserted.Conference_Day_ID
and Workshop_Reservation.Workshop_ID = inserted.Workshop_ID
and Workshop_Reservation.Is_Cancelled = 0
), 0)
begin
update Workshop_In_Day set Participants_Limit = deleted.Participants_Limit
from dbo.Workshop_In_Day
inner join deleted
on Workshop_In_Day.Conference_Day_ID = deleted.Conference_Day_ID
and Workshop_In_Day.Workshop_ID = deleted.Workshop_ID
raiserror ('You cannot set participants limit to value smaller than current reservations', 16, 1);
rollback transaction;
return;
end
end
end
create or alter trigger ConcurrentWorkshopParticipation
on Workshop_Participant
instead of insert
as
begin
if exists(select *
from inserted
inner join Workshop_reservation as outerRes
on outerRes.Workshop_Reservation_ID = inserted.Workshop_Reservation_ID
inner join Workshop_In_Day as outerWork
on outerWork.Conference_Day_ID = outerRes.Conference_Day_ID
and outerWork.Workshop_ID = outerRes.Workshop_ID
where exists(
select *
from Workshop_Participant
inner join Workshop_reservation
on Workshop_reservation.Workshop_Reservation_ID = Workshop_Participant.Workshop_Reservation_ID
and Workshop_Reservation.Is_Cancelled = 0
inner join Workshop_In_Day
on Workshop_In_Day.Conference_Day_ID = Workshop_reservation.Conference_Day_ID
and Workshop_In_Day.Workshop_ID = Workshop_reservation.Workshop_ID
where
Workshop_Participant.Person_ID = inserted.Person_ID
and Workshop_In_Day.Conference_Day_ID = outerWork.Conference_Day_ID
and outerWork.[From] between Workshop_In_Day.[From] and Workshop_In_Day.[To]
))
begin
raiserror ('You cannot reserve a workshop if you already reserved another workshop at the same time', 16, 1);
rollback transaction;
return ;
end
else
begin
insert into Workshop_Participant(
Person_ID,
Workshop_Reservation_ID)
select Person_ID, Workshop_Reservation_ID
from inserted
end
end