使用RAISERROR终止SQL语句 [英] SQL Statement Termination using RAISERROR

查看:244
本文介绍了使用RAISERROR终止SQL语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(SQL 2005)
raiserror是否可以终止存储的proc。

(SQL 2005) Is it possible for a raiserror to terminate a stored proc.

例如,在大型系统中,预期没有输入到特定列的值。在更新触发器中,如果您输入:

For example, in a large system we've got a value that wasn't expected being entered into a specific column. In an update trigger if you write:

如果存在(在插入的testcol = 7处选择*)
开始
raiseerror('My Custom Error ',16,1)
结尾

if exists (select * from inserted where testcol = 7) begin raiseerror('My Custom Error', 16, 1) end

更新信息仍然适用。
,但是如果您运行

the update information is still applied. however if you run

如果存在(从testcol = 7的插入位置选择*)
开始
选择1/0
end

if exists (select * from inserted where testcol = 7) begin select 1/0 end

抛出一个除以0的错误,该错误实际上终止了更新。
有什么办法可以通过raiseerror做到这一点,以便我可以返回自定义错误消息?

a divide by 0 error is thrown that actually terminates the update. is there any way i can do this with a raiseerror so i can get custom error messages back?

推荐答案

在触发器,请发出ROLLBACK,RAISERROR,然后返回。

In a trigger, issue a ROLLBACK, RAISERROR and then RETURN.

请参见 SQL Server中的错误处理-Erland Sommarskog触发的上下文

这篇关于使用RAISERROR终止SQL语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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