Sunday, February 25, 2007

Sql Server 2005: Change schema for all tables

I recently needed to change the schema for all the tables in a Sql Server 2005 database.
Sql Server 2005 provides a T-SQl statement capable to change the schema for a given object called ALTER SCHEMA
You may use the following syntax to change the schema for all the tables in a database. Just change the "new_schema" string with the desired one:

exec sp_MSforeachtable "ALTER SCHEMA new_schema TRANSFER ? PRINT '? modified' "


Enjoy!

9 comments:

Anonymous said...

Thanks a lot! It worked great!
Renato - Brazil

Ruslan Trifonov said...

I'm glad to hear that! :)

Anonymous said...

After "banging my head" one at a time through the first 20 tables...

Did a search found your script, THANKS!!!

U Rock!

Anonymous said...

thanks! this is 100000K helpful!!

:-)

Diesel said...

Man, I've been fighting with this for 2 hours, and this one line fixed the problem in 10 seconds. Thanks!

Lina said...

Nicely done! It worked for me too!

Stitchface. said...

Nice one Ruslan, worked a treat! Many thanks :)

Anonymous said...

Many thanks! That really helped me.
slashi@gmail.com

5pinx said...

Awesome command.