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.productid
use 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.categoryid
if 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触发器。
新闻热点
疑难解答