use northwind create trigger orddet_insert on 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
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
select * into customersger from customers where customers.country = 'germany' select * into customersmex from customers where customers.country = 'mexico'
go
在该数据上创建视图:
create view customersview as select * from customersger union select * from customersmex go
创建一个在上述视图上的instead of触发器:
create trigger customers_update2
on customersview
instead of update as
declare @country nvarchar(15)
set @country = (select country from inserted)
if @country = 'germany'
begin
update customersger
set customersger.phone = inserted.phone
from customersger join inserted
on customersger.customerid = inserted.customerid
end
else
if @country = 'mexico'
begin
update customersmex
set customersmex.phone = inserted.phone
from customersmex join inserted
on customersmex.customerid = inserted.customerid
end
通过更新视图,测试触发器:
update customersview set phone = ' 030-007xxxx' where customerid = 'alfki'
select customerid, phone from customersview where customerid = 'alfki'
select customerid, phone from customersger where customerid = 'alfki'
那么具体的讲,对于多列数据,如何计算方差呢?:
create trigger on dbo.dclb for insert,update as update p set /**//* 计算方差的触发器 */ p.t1=(i.p1+i.p2+i.p3+i.p4+i.p5+i.p6), p.t2=(i.y1+i.y2+i.y3+i.y4+i.y5+i.y6 ), p.t3=sqrt(p.t1*p.t1+p.t2*p.t2)
from dclb as p inner join inserted as i on p.sid = i.sid