use northwind
create trigger orddet_insert
on [order details]
for insert
as
update p set
unitsinstock = p.unitsinstock – i.quantity
from products as p inner join inserted as i
on p.productid = i.productiduse northwind
create trigger category_delete
on categories
for delete
as
update p set discontinued = 1
from products as p inner join deleted as d
on p.categoryid = d.categoryidif update (<column_name>)use northwind
go
create trigger employee_update
on employees
for update
as
if update (employeeid)
begin 
raiserror ('transaction cannot be processed./
***** employee id number cannot be modified.', 10, 1)
rollback transaction
end 
   instead of觸發器的工作過程
   可以在表或視圖上指定instead of觸發器。執行這種觸發器就能夠替代原始的觸發動作。instead of觸發器擴展了視圖更新的類型。對于每一種觸發動作(insert、update或 delete),每一個表或視圖只能有一個instead of觸發器。
   instead of觸發器被用于更新那些沒有辦法通過正常方式更新的視圖。例如,通常不能在一個基于連接的視圖上進行delete操作。然而,可以編寫一個instead of delete觸發器來實現刪除。上述觸發器可以訪問那些如果視圖是一個真正的表時已經被刪除的數據行。將被刪除的行存儲在一個名為deleted的工作表中,就像after觸發器一樣。相似地,在update instead of觸發器或者insert instead of觸發器中,你可以訪問inserted表中的新行。
   不能在帶有with check option定義的視圖中創建instead of觸發器。
新聞熱點
疑難解答