Tuesday, November 10, 2009

Alter object schema in SQL Server

I was using a third party tool recently to copy data into SQL Server 2005. The tool did not allow you to specify the schema of the tables it would create but would default to dbo. After importing the data, I then had to change the schema of the newly created tables to fit in with the rest of the database. This is achieved in SQL 2005 with a simple ALTER SCHEMA as follows.

ALTER SCHEMA new_schema TRANSFER dbo.tablename

where new_schema is an existing schema.

To achieve the same result in SQL 2000 you must use the sp_changeobjectowner stored procedure, passing in the object name and the new schema name as follows:

EXEC sp_changeobjectowner 'objectname', 'new_schema'

No comments:

Post a Comment