Let me start of by saying that I know SQL 2005 doesn't support the THROW feature.
I need a SP to be run on SQL Server 2012 and 2005. I would like to use the following section of code to allow SQL server generate a different error depending on the version of SQL server. But I am getting an error when trying to create the SP on the SQL 2005 server. Is there any way to get around this error when installing the SP?
Code:
IF LEFT(@@VERSION, 25) LIKE ('%2012')
BEGIN
;THROW 50013, @FullErrorMessage, 1
END
ELSE IF LEFT(@@VERSION, 25) LIKE ('%2005')
BEGIN
RAISERROR(@FullErrorMessage, 11, 1)
END
Error:
Msg 102, Level 15, State 1, Procedure SPNAME, Line xxxx
Incorrect syntax near 'THROW'.
Best Answer
Please use a dynamic statement:
IF LEFT(@@VERSION, 25) LIKE ('%2012') BEGIN EXEC sp_executesql N';THROW 50013, @FullMessage , 1','@FullMessage nvarchar(1000)',@FullMessage=@FullErrorMessage END ELSE IF LEFT(@@VERSION, 25) LIKE ('%2005') BEGIN RAISERROR(@FullErrorMessage, 11, 1) END