在Sql Server 2008中使用@@ Error [英] Use of @@Error in Sql Server 2008

查看:117
本文介绍了在Sql Server 2008中使用@@ Error的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 -  T-Sql块
开始
删除 作者中删除其中 au_id = '  172-32-1176'
声明  @ d   int  = @@ ERROR
更新作者 set  au_lname = NULL  where  au_id = '  172-32-1176'
声明  @ u   int  = @@ ERROR
如果 @ d<> 0
打印 ' 删除时发生错误'
如果 @ u<> 0
打印 ' 更新发生错误'
 



我得到的输出如下.在这里,以及使用@@ Error提出的自定义错误消息,我也收到了系统错误消息.那么有什么办法可以抑制系统错误消息?

报价:


消息547,级别16,状态0,第1行
DELETE语句与REFERENCE约束"FK__titleauth__au_id__0519C6AF"冲突.在数据库"pubs"的表"dbo.titleauthor"的列"au_id"中发生了冲突.
该声明已终止.
消息515,第16级,州2,第3行
无法将值NULL插入表"pubs.dbo.authors"的"au_lname"列中;列不允许为空. UPDATE失败.
该声明已终止.
删除发生错误
在更新中发生错误

解决方案

AFAIK除了溢出和相关的算术错误外,没有通用的方法来抑制SQL错误消息.TSQL中使用TRY CATCH块.请阅读这些页面以获取更多信息:

http://www.4guysfromrolla.com/webtech/041906-1.shtml [ ^ ]

http://msdn.microsoft.com/en-us/library/ms179296.aspx [ ^ ]

并使用Google查找有关此主题的更多信息.

希望对您有所帮助.


--T-Sql Block
go
delete from authors where au_id='172-32-1176'
declare @d int =@@ERROR
update authors set au_lname=NULL where au_id='172-32-1176'
declare @u int =@@ERROR
if @d<>0
print 'Error occured in delete'
if @u<>0
print 'Error occured in update'
go



The output i got is given below. Here along with the custom error messages that i am raising using the @@Error, I am also getting the system error messages. So is there any way we can suppress the system error messages

Quote:


Msg 547, Level 16, State 0, Line 1
The DELETE statement conflicted with the REFERENCE constraint "FK__titleauth__au_id__0519C6AF". The conflict occurred in database "pubs", table "dbo.titleauthor", column ''au_id''.
The statement has been terminated.
Msg 515, Level 16, State 2, Line 3
Cannot insert the value NULL into column ''au_lname'', table ''pubs.dbo.authors''; column does not allow nulls. UPDATE fails.
The statement has been terminated.
Error occured in delete
Error occured in update

解决方案

AFAIK there is no general way to suppress error messages in SQL, with the exception of overflow and related arithmetic errors.


You can use TRY CATCH block in your TSQL. Please read these pages for more info :

http://www.4guysfromrolla.com/webtech/041906-1.shtml[^]

http://msdn.microsoft.com/en-us/library/ms179296.aspx[^]

And find more about this topic by using Google.

Hope it helps.


这篇关于在Sql Server 2008中使用@@ Error的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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