T-SQL语句创建触发器

create trigger 触发器名
on 表或视图
for|after|instead of --操作时机
insert,update,delete
as
sql语句

 


例1:
 要求:在order_test表建立insert触发器,当向order_test表插入一行,如果cust_test表中对应

 记录status值为1,说明处于准备状态不能写入该数据

 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


例2:

在order_test表上建立一个插入触发器,在添加一个订单时,减少cust_test表的相应货物的记录的库存量。

create trigger cust_orders_ins3
on order_test
after insert
as
update cust_test set cstorage=cstorage-inserted.orders
from cust_test,inserted
where cust_test.customerid=inserted.customerid


例3:

 在order_test表上建立一个插入触发器,规定订单日期(Odate)不能手工修改。

create trigger orderdateupdate
on order_test
after update
as
if update (odate)
begin
raiserror('Error',10,1)
rollback transaction
end


例4:

 要求订购的物品一定要在仓库中有的,并且数量足够。

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 from cust_test,inserted
where cust_test.customerid=inserted.customerid)<
(select inserted.orders from cust_test,inserted
where cust_test.customerid=inserted.customerid)
begin
print 'No enough entry in goods for your order'
rollback transaction
end


例6:

 在order_test表上建立一个插入触发器,同时插入多行数据时,要求订购的物品一定要在仓库中有的


create trigger order_insert6
on order_test
after insert
as
if
(select count(*) from 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

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
1. 创建触发器: ``` --1. CREATE TRIGGER tr_insert_book ON books AFTER INSERT AS BEGIN PRINT '您又添加了一本书' END --2. CREATE TRIGGER tr_insert_book2 ON books AFTER INSERT AS BEGIN PRINT '您再次添加了一本书' END --3. CREATE TRIGGER tr_update_borrow_num ON borrowinfo AFTER INSERT AS BEGIN UPDATE BorrowNum SET num = (SELECT COUNT(*) FROM borrowinfo WHERE reader_id = inserted.reader_id) WHERE reader_id = inserted.reader_id END ``` 2. 触发器顺序: ``` CREATE TRIGGER tr_insert_book ON books AFTER INSERT AS BEGIN PRINT '触发器1' ROLLBACK END CREATE TRIGGER tr_insert_book2 ON books AFTER INSERT AS BEGIN PRINT '触发器2' END INSERT INTO books (book_id, book_name, author, publish_date, price) VALUES (1, 'book1', 'author1', '2021-10-01', 10) ``` 结果为:触发器2,触发器1 3. 查看和修改触发器: 在 SSMS 中,可以在数据库的“触发器”文件夹下找到已创建触发器,右键点击触发器选择“脚本触发器”可以查看和修改触发器。 4. 使用存储过程查看触发器: ``` --1. 查看所有触发器 EXEC sp_helptrigger 'books' --2. 查看指定触发器 SELECT OBJECT_NAME(parent_object_id) AS table_name, name AS trigger_name, OBJECT_DEFINITION(OBJECT_ID) AS definition FROM sys.triggers WHERE parent_class_desc = 'OBJECT_OR_COLUMN' AND OBJECT_NAME(parent_object_id) = 'books' AND name = 'tr_insert_book' ``` 5. 启用、禁用和删除触发器: ``` --禁用触发器 DISABLE TRIGGER tr_insert_book ON books --启用触发器 ENABLE TRIGGER tr_insert_book ON books --删除触发器 DROP TRIGGER tr_insert_book ON books ``` 6. 创建 DDL 触发器: ``` --创建数据库后触发器 CREATE TRIGGER tr_create_db ON ALL SERVER FOR CREATE_DATABASE AS BEGIN PRINT '数据库已创建' END --修改数据库后触发器 CREATE TRIGGER tr_alter_db ON ALL SERVER FOR ALTER_DATABASE AS BEGIN PRINT '数据库已修改' END --删除数据库后触发器 CREATE TRIGGER tr_drop_db ON ALL SERVER FOR DROP_DATABASE AS BEGIN PRINT '数据库已删除' END ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值