Skip to content
Advertisement

T-SQL : Alter Database statement inside IF is failing

We have a scenario where we have to change database compatibility based on SQL Server version and we have written below condition for the same.

In one of our customer’s server this code is failing with below error and the server has SQL Server 2014

Msg 15048, Level 16, State 1, Line 4
Valid values of the database compatibility level are 100, 110, or 120.

i.e. even though the condition is false, the Alter Database statement is still executing.

Tested the same condition with below code and the condition worked, it did not returned NULL as the server has SQL Server 2014 instead of 2016

So, why is the Alter Database statement being executed all the time even if the condition is false?

I believe, ideally it should not even enter OR pass the IF condition as it is checking for SQL Server 2016 but in actual the script is running on SQL Server 2014.

Advertisement

Answer

This is parsing error; SQL server is parsing you want level 130 and erroring before it even runs the statements. I can replicate the issue on an older server with the following:

Note that the ALTER DATABASE statement could never be run, yet this will produce the same error.

Instead, defer the validation of the statement by running it in a separate scope, if needed:

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement