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.
VijayAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Nitin SontakkeDeveloperCommented:
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

0
Nitin SontakkeDeveloperCommented:
Incidently, sp_attach_db is presumably now deprecated, use this instead:

https://docs.microsoft.com/en-us/sql/relational-databases/databases/attach-a-database
0
Nitin SontakkeDeveloperCommented:
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

0
VENKAT KOKULLASQL Server DBACommented:
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
VijayAuthor Commented:
Thank you Venkat.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Databases

From novice to tech pro — start learning today.