-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTriggerExample.sql
More file actions
59 lines (43 loc) · 1.66 KB
/
TriggerExample.sql
File metadata and controls
59 lines (43 loc) · 1.66 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
--After insert Order
CREATE Trigger trg_GetInsertedOrder on [Order Details] -- CREATED Trigger
After insert
As
Begin
Declare @quantity Int, @productId int; --you defined parameter and type
select @quantity=Quantity, @productId=ProductID from inserted
Update Products
Set UnitsInStock=UnitsInStock- @quantity
where ProductID=@productId
End
Insert into [Order Details] (OrderId,ProductID,UnitPrice,Quantity) values (11079,4,22,3)
select ProductID,ProductName,UnitsInStock from Products where ProductID=1
--After Delete Order
CREATE Trigger trg_GetStockBack on [Order Details]
after delete
as
begin
Declare @quantity Int, @productId int;
select @quantity=Quantity, @productId=ProductID from deleted
Update Products
Set UnitsInStock=UnitsInStock+@quantity
where ProductID=@productId
end
Delete from [Order Details] where OrderID=11079 and Quantity=3
select ProductID,ProductName,UnitsInStock from Products where ProductID=4
--After Update Order
create trigger trg_StockUpdate on [Order Details]
after update
as
begin
declare @oldquantity smallint, @newquatity smallint, @productId int;
select @oldquantity=Quantity,@productId=ProductID from deleted
select @newquatity=Quantity from inserted
update Products
set UnitsInStock+= @oldquantity-@newquatity
where ProductID=@productId
end
update [Order Details] set Quantity=7 where OrderID=11079 and ProductID=4
select ProductID,ProductName,UnitsInStock from Products where ProductID=4
select * from [Order Details] where OrderID = 11079
--Delete Trigger
drop trigger trg_StockUpdate