How do I move a table into a schema in T-SQL

Lukasz picture Lukasz · Jul 19, 2009 · Viewed 162.8k times · Source

I want to move a table into a specific Schema using T-SQL? I am using SQL Server 2008.

Answer

Mitch Wheat picture Mitch Wheat · Jul 19, 2009
ALTER SCHEMA TargetSchema 
    TRANSFER SourceSchema.TableName;

If you want to move all tables into a new schema, you can use the undocumented (and to be deprecated at some point, but unlikely!) sp_MSforeachtable stored procedure:

exec sp_MSforeachtable "ALTER SCHEMA TargetSchema TRANSFER ?"

Ref.: ALTER SCHEMA

SQL 2008: How do I change db schema to dbo