Link to home
Start Free TrialLog in
Avatar of Zack
ZackFlag for Australia

asked on

Changing the Schema for all Tables in a Tables - MS SQL Server 2008 R2.

Hi EE,

I know to change the schema name of table you the following command:

ALTER SCHEMA MySchema TRANSFER dbo.MyTable

My query are there any scripts out there that do this to all the tables in a database as this database has hundreds of tables?

Any assistance is appreciated.

Thank you.
Avatar of Kelvin Sparks
Kelvin Sparks
Flag of New Zealand image

I'd create a loop based on the tables names returned from something like SELECT Name from sys.Tables and then generate dynamic sql like

'ALTER SCHEMA myschema TRANSFER ' + dbo.Mytable

then execute it.

Sorry, not at a computer to code it exactly.


Kelvin
ASKER CERTIFIED SOLUTION
Avatar of JesterToo
JesterToo
Flag of United States of America 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 Zack

ASKER

Thank you very much for the link.