-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTriggers.sql
36 lines (34 loc) · 933 Bytes
/
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
----Trigger to update stock when an order is placed
create trigger trg_ConfirmStock on OrderDetail after insert as
begin update Book
set bkStock = bkStock - (
select odQuantity
from inserted
where odBookId = bkId
)
from Book
join inserted on Book.bkId = inserted.odBookId
end
go
----Trigger to update stock when an ongoing order is canceled
create trigger trg_CancelOrder on OrderDetail for delete as
begin update Book
set bkStock = bkStock + (
select odQuantity
from deleted
where odBookId = bkId
)
from Book
join deleted on Book.bkId = deleted.odBookId
end
go
----Trigger to update stock after updating order quantity
create trigger trg_QuantityUpdate on OrderDetail after update as
begin update Book
set bkStock = bkStock -
(select odQuantity from inserted where odBookId = bkId) +
(select odQuantity from deleted where odBookId = bkId)
from Book
join deleted on Book.bkId = deleted.odBookId
end
go