Tuesday, March 27, 2012

@@ERROR is not being populated

Hi All,

I ran the code below in QA, and @.@.ERROR never gets populated with any error code, despite the fact that an error occured. This event is very misleading and errors are never caught. Any advice greatly appreciated.

-- Create the table
create table test_table (
test_field int
)

-- Execute the code
DECLARE @.err_status int, @.row_count int
insert into test_table (
test_field
)
values (
'TEST STRING'
)
select @.err_status = @.@.ERROR, @.row_count = @.@.ROWCOUNT

IF @.err_status <> 0 PRINT @.err_status

-- Results:
Server: Msg 245, Level 16, State 1, Line 1
Syntax error converting the varchar value 'TEST STRING' to a column of data type int.

The reason is that the statements after insert never get executed. Some TSQL errors abort the batch execution or compile-time errors for example. Please check out the links below for more information:

http://www.sommarskog.se/error-handling-I.html

http://www.sommarskog.se/error-handling-II.html

No comments:

Post a Comment