Sunday, 16 August 2015

SQL Server set compatibility level for all server databases

use master;
go


DECLARE UserDatabases_CTE_Cursor Cursor
FOR

-- Selecting user database names.
select name as DatabaseName
from sys.sysdatabases
where ([dbid] > 6) and ([name] not like '$')

OPEN UserDatabases_CTE_Cursor
DECLARE @dbName varchar(100);

DECLARE @compatQuery varchar(500);



Fetch NEXT FROM UserDatabases_CTE_Cursor INTO @dbName
While (@@FETCH_STATUS <> -1)

BEGIN
-- set database compatibility level
set @compatQuery =  'ALTER DATABASE ' + @dbName + ' SET COMPATIBILITY_LEVEL = 100;'

-- Print SQL statement
print @compatQuery

-- Execute compatability script
EXEC (@compatQuery)

-- Get next database
Fetch NEXT FROM UserDatabases_CTE_Cursor INTO @dbName
END

CLOSE UserDatabases_CTE_Cursor
DEALLOCATE UserDatabases_CTE_Cursor
GO

No comments:

Post a Comment