如何中止触发器中多行的插入 [英] How to abort an insert of multiple rows in a trigger

查看:48
本文介绍了如何中止触发器中多行的插入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 sql Server 2005.我已经声明了一个触发器,该触发器被触发 "AFTER INSERT, UPDATE" ,在这个触发器中,我使用 WHILE 和 CURSOR 在 INSERTED 表的行上循环.当我发现一行不满足特定条件时:

With sql Server 2005. I have declared a trigger that get fired "AFTER INSERT, UPDATE" , in this trigger I'm using a WHILE and a CURSOR to loop on the INSERTED table's rows. When I find a row that does not sotisfy a specific condition:

我希望触发器引发错误并且不插入任何触发触发器的行(甚至不插入那些已经满足我的条件的行).<--- 我不知道该怎么做!

I want the trigger to rise an error and do not insert any of the rows that fired the trigger (not even those that already satisfaied my condition). <--- I don't know how to do this!

你能告诉我如何提高错误并防止插入吗?

Can you tell me how can I rise the error and prevent the insertion?

推荐答案

使用回滚

IF <some condition>
BEGIN
   RAISERROR ('condition doesn't satisfy something', 16, 1)
   ROLLBACK TRANSACTION
END

这篇关于如何中止触发器中多行的插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆