轻松掌握设计 Sql Server触发器的原理
2007-07-31 09:47:15 来源:WEB开发网核心提示: (要求:在northwind表中建立2个表:cust_test和order_test)cust_test: CustomerID char(5) PKorder_test:CustomerID char(5)--对应关系Custcity OrderidPKCustname OrderNam
(要求:在northwind表中建立2个表:cust_test和order_test)
cust_test: CustomerID char(5) PK order_test:
CustomerID char(5)--对应关系
Custcity Orderid PK
Custname OrderNames
CStatus int OStatus int --状态
Cstorage int Orders int --定购量和库存量
Cdate date Odate date--日期
作业1:
在cust_test表中建立删除触发器,实现上述2表的级联删除。
作业2:
在order_test表建立insert触发器,当向order_test表插入一行,如果cust_test表中对应
记录status值为1,说明处于准备状态不能写入该数据。
答案1:
use northwind
go
create trigger cust_orders_del1
on Cust_test
after delete
as
delete from order_test
where CustomerID in
(select CustomerID from deleted)
go
答案2:
use northwind
go
create trigger cust_orders_ins2
on order_test
after insert
as
if (select cstatus from cust_test,inserted where
cust_test.customerid=inserted.customerid)=1
begin
print 'The Goods is being processed'
rollback transaction
end
go
图形化操作触发器
11.3 查看触发器情况
图形化操作结合T-SQL命令
(1)sp_helptrigger 触发器名
查看触发器的名称,拥有者和五个布尔值
supdate,isdelete,isinsert,isafter,isinsteadof
更多精彩
赞助商链接