Moving Table To Another Schema In SQL Server

Moving table to another schema in SQL Server

Recently I created a table under a wrong schema. Later, I realized the problem and moved the table to the correct schema. Here, I have couple of sample script for moving table to another schema.

Moving one table to a schema

For transferring one table to another schema is by using ALTER SCHEMA transact SQL statement. Here is a sample script to transfer a table PurchaseOrder from its default dbo schema to a new schema Purchasing.

ALTER SCHEMA Purchasing 
  TRANSFER dbo.PurchaseOrders;
GO
Moving Table To Another Schema In SQL Server

Moving all tables to a schema

For moving all the tables to another schema at the same time then use the SP_MSFOREACHTABLE stored procedure. Here is the sample script.

EXEC SP_MSFOREACHTABLE "ALTER SCHEMA Purchasing TRANSFER ?"

Reference

Related article


Related Articles & Tools

Leave your thoughts...

This site uses Akismet to reduce spam. Learn how your comment data is processed.