Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2016-07-15
4
Medium Priority
?
344 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 27

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 2000 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 27

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

916 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