Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have two schema. Schema1 and Schema2

How would I transfer all the tables from schema1 to schema2?

share|improve this question
up vote 4 down vote accepted

Execute this for all of the tables:

alter schema2
transfer schema1.table1;

To programmatically get the statements to execute:

    @SourceSchemaName sysname,
    @DestinationSchemaName sysname,
    @AlterStatements varchar(max);

set @SourceSchemaName = 'NewSchema';
set @DestinationSchemaName = 'HumanResources';
set @AlterStatements = '';

    @AlterStatements +=
        'alter schema ' + quotename(@DestinationSchemaName) + 
        ' transfer ' + quotename(schema_name(schema_id)) + '.' + quotename(object_name(object_id)) +
        char(13) + char(10)
from sys.tables
where schema_id = schema_id(@SourceSchemaName);

print @AlterStatements;
share|improve this answer
I have a thousand tables. – Emman Bangis Jul 9 '14 at 18:46
I edited my answer to show code to generate the ALTER statements. – Thomas Stringer Jul 9 '14 at 18:46

Assuming destination schema is empty:


SELECT @sql += N'
  + QUOTENAME( + ';' FROM sys.tables AS t
  INNER JOIN sys.schemas AS s
  ON t.[schema_id] = s.[schema_id]
  WHERE = N'Schema1';

PRINT @sql;
-- EXEC sp_executesql @sql;

If the destination schema is not empty, you'll need additional handling for conflicts.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.