Solved

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

Posted on 2016-07-15
4
47 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Join & Write a Comment

Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

747 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now