【发布时间】:2013-11-27 15:04:36
【问题描述】:
我只是想确认我所看到的。下面是我写的一个小型概念存储过程证明,它说明了我在一个更大的问题中遇到的问题。
ALTER PROCEDURE TestErrorHandling
@Param1 varchar(1) = ''
AS
BEGIN
/*
Unit test:
DECLARE @returnStatus nvarchar(15);
Exec @returnStatus = TestErrorHandling
print @returnStatus
*/
BEGIN TRY
print 'Start'
IF @Param1 = '' raiserror( '@Param1 is missing', 18, 1 );
print 'Should not see this'
END TRY
BEGIN CATCH
print error_message()
print error_state()
print error_number()
INSERT INTO [FlightOrderUploadFailedLog]
(EvtTyp)
VALUES ('max size of EvtType is only varchar(15) so this should cause truncation')
print 'after insert '
print error_message()
print error_state()
print error_number()
return -- added in second version (after original post)
END CATCH
END
GO
输出:
开始
@Param1 is missing
1
50000
Msg 8152, Level 16, State 4, Procedure TestErrorHandling, Line 30
String or binary data would be truncated.
The statement has been terminated.
after insert
@Param1 is missing
1
50000
-8
令我惊讶的是,程序并没有因为“字符串或二进制数据将被截断”而崩溃。换句话说,之后的代码仍然运行。此外,error_message 不会被 catch 块内的 SQL 错误更改。
所以问题是 - 在“BEGIN CATCH”部分处理意外错误的最佳做法是什么?我应该有另一个嵌套的 TRY/CATCH 吗?
注意:这是我最初的问题,但我认为把这个问题放在那里会使它脱离主题并混淆问题。这个得到回答后,我会回去更新那个:T-SQL Clear Errors
第 2 部分 - 稍后添加:
DECLARE @returnStatus nvarchar(15);
Exec @returnStatus = TestErrorHandling
print @returnStatus
这将返回 -8。 -8 到底是从哪里来的?
我还在尝试添加“return”与“return 0”。当 BizTalk 调用存储过程时,我希望他认为它已成功完成,以免每 5 分钟进行 3 次重试。
补充:我认为这主要是我正在寻找的答案: SQL try-catch statement not handling error (SQL Server 2008)
但它没有讨论我在这个问题中提出的最佳实践问题。
更新:这是演示艾伦响应的程序:
ALTER PROCEDURE TestErrorHandling2
@Param1 varchar(1) = ''
AS
BEGIN
/*
Unit test:
DECLARE @returnStatus nvarchar(15);
Exec @returnStatus = TestErrorHandling2
print @returnStatus
This is proof of concept on error handling.
See question: https://stackoverflow.com/questions/20245900/t-sql-is-error-handling-totally-turned-off-in-a-begin-catch-block
Testing to see if Truncation error stops or not.
*/
print 'Start'
INSERT INTO [FlightOrderUploadFailedLog]
(EvtTyp)
VALUES ('max size of EvtType is only varchar(15) so this should cause truncation')
print 'after insert #1'
print 'Message=' + IsNull(error_message(),'null')
print 'State=' + IsNull(convert(varchar(4),error_state()),'null')
print 'ErrorNumber=' + IsNull(convert(varchar(8),error_number()),'null')
BEGIN TRY
print 'Start - Begin Try'
INSERT INTO [FlightOrderUploadFailedLog]
(EvtTyp)
VALUES ('max size of EvtType is only varchar(15) so this should cause truncation')
print 'after insert #2 '
print 'Message=' + IsNull(error_message(),'null')
print 'State=' + IsNull(convert(varchar(4),error_state()),'null')
print 'ErrorNumber=' + IsNull(convert(varchar(8),error_number()),'null')
print 'The End'
END TRY
BEGIN CATCH
print 'Catch'
print 'Message=' + error_message()
print 'State=' + convert(varchar(4),error_state())
print 'ErrorNumber=' + convert(varchar(8),error_number())
return -- error has been theoretically handled by writing it to a database
END CATCH
END
GO
结果:
Start
Msg 8152, Level 16, State 4, Procedure TestErrorHandling2, Line 21
String or binary data would be truncated.
The statement has been terminated.
after insert #1
Message=null
State=null
ErrorNumber=null
Start - Begin Try
(0 row(s) affected)
Catch
Message=String or binary data would be truncated.
State=4
ErrorNumber=8152
-6
【问题讨论】:
标签: sql-server-2008 tsql error-handling try-catch