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

No comments:

Post a Comment