0

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'.

2

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

2
  • Works!!! Thank you so much. That is genius! – Chandler R Reeves Jul 29 '16 at 19:25
  • I had another compilation issue recently because the procedure contained execute as login and the login that was calling the procedure did not have permission to use the linked server referenced inside of the procedure. So I created a procedure as a wrapper to call the one that used the linked server and placed the execute as on the wrapper and it worked. – Michael Keleher Jul 29 '16 at 20:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.