Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Alter Database Runs Irrespective of Is Exist Condi

Author  Topic 

gokula_k
Starting Member

3 Posts

Posted - 2015-03-05 : 22:30:36
Hi,
Problem:
I want to set compatibility_level only when it is greater than 110.

Solution:
Select the compatibility level and if it is greater than 110, I alter database set compatibility level=110

ISSUE
Irrespective of IF Exist statement the alter database statement is executed all the time.

Here is the sql statement
IF EXISTS (
SELECT * FROM sys.databases where compatibility_level >110 AND name='mydatabase'
)
BEGIN
ALTER DATABASE mydatabase SET COMPATIBILITY_LEVEL = 110
END

What is that I am missing here ??
please support.

seshurtz
Starting Member

7 Posts

Posted - 2015-03-08 : 09:53:39
Hi gokula_k,

I tested the script you provided and it appears to work as intended. Per my tests, it took into consideration the IF Exists statement.

Is this the only script your using or is there more to it?


Best,

Steve Shurtz, MCP
Database Administrator
Go to Top of Page
   

- Advertisement -