Link to home
Start Free TrialLog in
Avatar of Vijay
Vijay

asked on

T-SQL Scripts for DB migration

I am looking for scripts for below requirements:

1. How quickly attach 100+ databases in sql 2016?
2. How quickly change 100+ databases compatibility level (from 100 to 130) in sql 2016?

The exact requirement is that we are migrating sql server 2008 to 2016.
Avatar of Nitin Sontakke
Nitin Sontakke
Flag of India image

Very simple, if you know already how to use meta data or use scripts to generate scripts. Too lazy to to type everything here. Pick-up a hint if you can and experiment. The best approach.

declare @dynamicSQL nvarchar(max) = '';

select @dynamicSQL += 'execute sp_attach_db ''' + [name] + ''';
'
from sys.databases 
where 1 = 1
  and [database_id] > 4

select @dynamicSQL;

Open in new window

Incidently, sp_attach_db is presumably now deprecated, use this instead:

https://docs.microsoft.com/en-us/sql/relational-databases/databases/attach-a-database
declare @dynamicSQL nvarchar(max) = '';

select @dynamicSQL += 'alter database [' + [name] + ']
  set compatibility_level = 130;

'
from sys.databases 
where 1 = 1
  and [database_id] > 4

select @dynamicSQL;

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of VENKAT KOKULLA
VENKAT KOKULLA
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Vijay
Vijay

ASKER

Thank you Venkat.