Solved

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

Posted on 2016-07-15
4
194 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 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 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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

623 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