SQL SERVER – Move a Table From One Schema to Another Schema

ALTER SCHEMA can Move a Table between schemas within one database. A customer had imported under the wrong schema.

An intact vessel tray changes compartments within one cabinet beside preserved fittings.

-- Reviewed change template in the intended database:
-- ALTER SCHEMA NewSchema TRANSFER OldSchema.TableName;
SELECT s.name AS SchemaName, t.name AS TableName
FROM sys.tables t JOIN sys.schemas s ON s.schema_id=t.schema_id
ORDER BY s.name,t.name;

The destination schema must exist. The operator needs documented object and destination permissions. This changes the metadata container. It does not move data into another database or file.

References to OldSchema.TableName are not rewritten automatically. Inspect application SQL, procedures, views, synonyms and schema-bound dependencies. Transfer drops object-associated permissions. Preserve and deliberately restore required grants while assessing schema-level access.

Capture the original schema, dependencies and permissions. Test on a copy and validate the application afterward. Reversing the transfer alone does not necessarily restore the original access behavior.

Reference: Schema transfer references and permissions.

Related reading

A schema transfer is not an automatic reference repair, it is a qualified-name change with dependency and permission effects.

Published by Pinal Dave on SQLAuthority. More of my work at pinaldave.com.


Discover more from SQL Authority with Pinal Dave

Subscribe to get the latest posts sent to your email.

Schema, SQL Scripts, SQL Server
Previous Post
Generate a Date Range: List All Dates Between Two Dates
Next Post
Grid vs Text Output in SSMS: Why Line Breaks Disappear

Related Posts

3 Comments. Leave new

  • Marcos Yzquierdo
    May 27, 2021 9:11 pm

    Pinal, great post. Quick question, should the process to move a table from one schema to another take a long time? What if the table is being accessed? I suspect you would want to set the database to single user mode and then perform the schema change.

    Reply
  • How can I move all Tables in synapse

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.