練習建立觸發程序
Fresh Fruit Delivery 公司需要你的協助,在 Orders 資料表上建立一個名為 OrdersUpdatedRows 的新觸發程序。
這個觸發程序負責將已更新列的資訊,寫入一個歷史資料表(OrdersUpdate)。
歷史資料表常用來保存原始資料表被變更過的資訊。在這個例子中,訂單的異動將會儲存到 OrdersUpdate,供公司進行稽核使用。
本練習屬於課程
在 SQL Server 建立與最佳化觸發程序
練習說明
- 為
Orders資料表建立新的觸發程序。 - 將觸發程序設定為只在
UPDATE陳述式之後才執行。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
-- Set up a new trigger
___ TRIGGER OrdersUpdatedRows
ON ___
-- The trigger should fire after UPDATE statements
___ UPDATE
-- Add the AS keyword before the trigger body
AS
-- Insert details about the changes to a dedicated table
___ INTO OrdersUpdate(OrderID, OrderDate, ModifyDate)
SELECT OrderID, OrderDate, GETDATE()
FROM inserted;