-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy path46-Trigger.sql
More file actions
31 lines (26 loc) · 1 KB
/
46-Trigger.sql
File metadata and controls
31 lines (26 loc) · 1 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
-- Trigger khi insert sẽ chuyển productName về viết hoa toàn bộ:
CREATE TRIGGER MakeProductNameUpperCase
ON [dbo].[Products]
AFTER INSERT
AS
BEGIN
UPDATE Products
SET ProductName = UPPER(i.ProductName)
FROM inserted i
WHERE Products.ProductID = i.ProductID
END
INSERT [dbo].[Products]([ProductName], [SupplierID], [CategoryID])
VALUES ('test name', 1, 1)
-- Viết một trigger trong cơ sở dữ liệu Northwind để đảm bảo
-- rằng mỗi khi có một chi tiết đơn hàng mới được thêm vào, số lượng tồn kho phải được giảm đi
CREATE TRIGGER UpdateProductInventory
ON [dbo].[Order Details]
AFTER INSERT
AS
BEGIN
UPDATE [dbo].[Products]
SET [UnitsInStock] = [UnitsInStock]-(SELECT [Quantity] FROM inserted WHERE [dbo].[Products].ProductID = inserted.ProductID)
WHERE [dbo].[Products].[ProductID] IN (SELECT [ProductID] FROM inserted)
END
INSERT INTO [dbo].[Order Details]([OrderID], [ProductID], [UnitPrice], [Quantity], [Discount])
VALUES (10248, 78, 10, 50, 0);