Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

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

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

721 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