In SQL Server 2008 (but also in 2014). Let's consider a procedure that has an output parameter. This procedure may produce an error (and will in the following example). I note that the behaviour of the output parameter is not the same if we call the procedure within a TRY
/ CATCH
block.
Example:
create procedure test_output @result varchar(10) output
as
begin
set @result = 'hello'
raiserror('This is an error', 16,1)
set @result = 'error'
end
If we launch the procedure the simple way:
declare @res1 varchar(10)
exec test_output @result = @res1 out
print 'Result is: '+ isnull(@res1, 'empty')
we get (and I'm fine with it):
Msg 50000, Level 16, State 1, Procedure test_output, Line 7 [Batch Start Line 12]
This is an error
Result is: error
If the procedure is now in a try/catch block:
declare @res2 varchar(10)
declare @error_message varchar(max)
begin try
exec test_output @result = @res2 out
end try
begin catch
set @error_message = error_message()
raiserror(@error_message, 16,1)
print 'Result is: '+ isnull(@res2, 'empty')
end catch
we get (and I'm upset):
Msg 50000, Level 16, State 1, Line 28
This is an error
Result is: empty
The error message is OK, but the output parameter is now NULL. If, in a TRY...CATCH
context, the execution is halted immediately after the RAISERROR
, I would have expected the output value to be set to hello.
Why is it so?