《实验27 查看、修改和删除触发器》由会员分享,可在线阅读,更多相关《实验27 查看、修改和删除触发器(3页珍藏版)》请在金锄头文库上搜索。
1、放心减肥网 http:/ 提供放心减肥网 http:/ 提供实验 27 查看、修改和删除触发器实验目的:掌握触发器的创建及其注意事项。实验课时:2 课时实验方法:案例分析法实验内容:一、查看触发器 (1)sp_helptrigger 触发器名 查看触发器的名称,拥有者和五个布尔值 supdate,isdelete,isinsert,isafter,isinsteadof (2)sp_helptext 触发器名 查看文本信息 二、修改触发器重点演示如何设置触发器的无效和重新有效。 、无效: use northwind alter table 表名 disable trigger 触发器名 、重新
2、有效: use northwind alter table 表名 enable trigger 触发器名 三、删除触发器 语法格式为drop trigger 触发器名【例 1】在 order_test 表上建立一个插入触发器,在添加一个订单时,减少cust_test 表的相应货物的记录的库存量。参考代码:use northwind go create trigger cust_orders_ins3 on order_test after insert as update cust_test set cstorage=cstorage-inserted.orders from cust_tes
3、t,inserted where cust_test.customerid=inserted.customerid【例 2】在 order_test 表上建立一个插入触发器,规定订单日期(Odate)不能手工修改。 参考代码:use northwind 放心减肥网 http:/ 提供放心减肥网 http:/ 提供go create trigger orderdateupdate on order_test after update as if update (odate) begin raiserror(Error,10,1) rollback transaction end【例 3】要求订购
4、的物品一定要在仓库中有的,并且数量足够。参考代码:use northwind go create trigger order_insert5 on order_test after insert as begin if(select count(*) from cust_test,inserted where cust_test.customerid=inserted.customerid)=0 begin print No entry in goods for your order rollback transaction end if(select cust_test.cstorage f
5、rom cust_test,inserted where cust_test.customerid=inserted.customerid)rowcount -可以在触发器逻辑中使用 ROWCOUNT 函数以区分单行插入和多行插入。 begin delete order_test from order_test,inserted where order_test.orderid=inserted.orderid and inserted.customerid not in (select customerid from cust_test) end print rowcount 说 明 设置 SET ROWCOUNT 选项将使大多数 Transact-SQL 语句在已受指定数目的行影响后停止处理。这包括触发器和 INSERT、UPDATE 及 DELETE 等数据修改语句。ROWCOUNT 选项对动态游标无效,但限制键集的行集和不感知游标。使用该选项时应谨慎,它主要与 SELECT 语句一起使用。