HiveBrain v1.2.0
Get Started
← Back to all entries
debugsqlMinor

Is it possible to choose RAISERROR or THROW depending on SQL Server version?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
raiserrorserversqlchoosedependingversionpossiblethrow

Problem

Here's my code right now:

BEGIN TRY
INSERT INTO TABLE (F1,F2,F3) 
VALUES ('1','2','3')
END TRY
BEGIN CATCH
;THROW
END CATCH


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.

BEGIN CATCH
IF ((SELECT SERVERPROPERTY('productversion')) >= 11) ;THROW
END CATCH


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