I am using a try-catch block in T-SQL, and I want to only catch a specific error number. In other cases, I am using RAISERROR() as a poor-man’s re-throw to return error information to the application.
When I try the following, I get an “Incorrect syntax near ‘error_message'” error:
raiserror
(
error_message()
,1
,1
)
The following, however, works fine:
declare @err varchar(100)
set @err = error_message()
raiserror
(
@err
,1
,1
)
I thought it might be a typecasting quirk, so I tried this, but that also yielded a syntax error:
raiserror
(
cast(error_message() as varchar(100))
,1
,1
)
What’s going on here? Why do I have to store the result of ERROR_MESSAGE() in a variable before using it as a parameter to RAISERROR(), instead of calling the function directly?
Below post answers your Question: https://stackoverflow.com/a/3415125/639960
In a nutshell (quoted from above post):
See Executing Stored Procedures for documentation on this.