debugsqlMinor
Is it possible to choose RAISERROR or THROW depending on SQL Server version?
Viewed 0 times
raiserrorserversqlchoosedependingversionpossiblethrow
Problem
Here's my code right now:
Works great, unless it's run on a machine with SQL 2008. I'd like to have the CATCH block do a check against the SQL version and run THROW if it's equal or higher to 2012, and RAISERROR if it's 2008. I keep running into syntax errors, and I'm wondering if it's even possible. Even something simple like this is not working for me.
Any advice is appreciated.
BEGIN TRY
INSERT INTO TABLE (F1,F2,F3)
VALUES ('1','2','3')
END TRY
BEGIN CATCH
;THROW
END CATCHWorks great, unless it's run on a machine with SQL 2008. I'd like to have the CATCH block do a check against the SQL version and run THROW if it's equal or higher to 2012, and RAISERROR if it's 2008. I keep running into syntax errors, and I'm wondering if it's even possible. Even something simple like this is not working for me.
BEGIN CATCH
IF ((SELECT SERVERPROPERTY('productversion')) >= 11) ;THROW
END CATCHAny advice is appreciated.
Solution
No this isn't possible.
This is invalid syntax in earlier versions and will cause a compile error.
It is not possible to hide the
You would need to deploy the code version you want according to the version of SQL Server you are deploying to (and unfortunately there is not good support for this either in the SSDT tooling that I am aware of - no equivalent of including code lines selectively through conditional compilation)
This is invalid syntax in earlier versions and will cause a compile error.
It is not possible to hide the
THROW in an EXEC inside the catch block either as a parameterless throw must be directly contained inside the catch.You would need to deploy the code version you want according to the version of SQL Server you are deploying to (and unfortunately there is not good support for this either in the SSDT tooling that I am aware of - no equivalent of including code lines selectively through conditional compilation)
Context
StackExchange Database Administrators Q#157790, answer score: 9
Revisions (0)
No revisions yet.