Solved

script to change compatibility level on all databases if level equal 90

Posted on 2016-07-15
4
83 Views
Last Modified: 2016-07-18
Hello,

I search a script to put all databases SINGLE_USER, change the compatibility level to 100 if it is equal to 90 and put all databases MULTI_USER

Thanks

Regards
0
Comment
Question by:bibi92
  • 2
4 Comments
 
LVL 26

Expert Comment

by:Zberteoc
ID: 41714550
This script will generate the commands to do that. Run it in Management Studio in a Query Window and then in the result panel select the result and copy it with no headers by right clicking on it and choose the "Copy" option or just hit Ctrl+C on your keyboard after you selected it. Open a new Query Window, paste the result in it and then execute the entire script.

USE [master]
GO

declare
	@sql varchar(8000)=''
select 
	@sql=@sql+'-- ### changing ['+[name]+'] database 	
ALTER DATABASE ['+[name]+'] SET SINGLE_USER WITH ROLLBACK IMMEDIATE
GO
ALTER DATABASE ['+[name]+'] SET compatibility_level = 100;
GO
ALTER DATABASE ['+[name]+'] SET MULTI_USER WITH NO_WAIT
GO

'
from 
	sys.databases 
where
	[compatibility_level]=90

select @sql='USE [master]
GO

'+@sql

select @sql

Open in new window

0
 
LVL 16

Accepted Solution

by:
DcpKing earned 500 total points
ID: 41714615
Run this from Master:
declare @ta table (	dBaseName varchar(64), dBaseID int, comLevel varchar(8), 
					Access varchar(32), State varchar(32), ReuseWait varchar(32))
--	--------------------------------------------
insert into @ta
	select name, Database_Id, compatibility_level, user_access_desc, state_Desc, log_reuse_wait_desc 
	FROM sys.databases;  
--	--------------------------------------------
declare @strDatabaseName varchar(64),
		@strSQL varchar(500)
declare db_cursor cursor for  
	select dBaseName from @ta where comLevel = '90' and dBaseID > 4  --and any other exclusions you want ...
--	--------------------------------------------
open db_cursor   
fetch next from db_cursor INTO @strDatabaseName
--	--------------------------------------------
while @@FETCH_STATUS = 0   
begin   
	set @strSQL = 'alter database [' + @strDatabaseName + '] set SINGLE_USER WITH ROLLBACK IMMEDIATE;'
	exec(@strSQL)
	set @strSQL = 'alter database [' + @strDatabaseName + '] set compatibility_level = 100;'
	exec(@strSQL)
	set @strSQL = 'alter database [' + @strDatabaseName + '] set MULTI_USER;'
	exec(@strSQL)
fetch next from db_cursor INTO @strDatabaseName
end
close db_cursor
deallocate db_cursor

Open in new window


hth

Mike
0
 

Author Closing Comment

by:bibi92
ID: 41717041
Thanks
0
 
LVL 26

Expert Comment

by:Zberteoc
ID: 41717378
No offence, but what was wrong with my script? I posted it before
DcpKing. Have you even tried it?
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question