SQL Server: Transfer Objects From One Schema to Another

This post describes how to transfer objects from one schema to another. A simple method is to make use of the ALTER Schema statement as shown below.

Consider that there are two schemas named test1 and test2 in the database and you want
to move tables from schema test1 to test2.

Let’s create a table in schema test1.

image

In the above query, we have also run a query to know which schema the table test belongs to.

create table test1.test(id int, names varchar(100))
select * from information_schema.tables where TABLE_NAME='test'

schema3

As you can see, the schema of the table is test1

Now run the following query to move table test from schema test1 to test2

schema4

Again run the following statement to know which schema the table test now belongs to

SELECT * from information_schema.tables where TABLE_NAME='test'

schema6

As you see from the result, the schema of table has now changed from test1 to test2


About The Author

Madhivanan,an MSc computer Science graduate from Chennai-India, works as a works as a Lead Subject Matter Expert at a company that simplifies BIG data. He started his career as a developer working with Visual Basic 6.0, SQL Server 2000 and Crystal Report 8. As years went by, he started working more on writing queries in SQL Server. He now has good level of knowledge in SQLServer, Oracle, MySQL and PostgreSQL as well. He is also one of the leading posters at www.sqlteam.com and a moderator at www.sql-server-performance.com. His T-sql blog is at http://beyondrelational.com/blogs/madhivanan

No comments: