Published on

August 30, 2018

Renaming Schemas in SQL Server

Schemas are a powerful tool in SQL Server that can be used to organize and manage database objects. They allow you to group related objects together and provide a way to separate different functionalities within a database.

In some organizations, schemas are extensively used, with each schema serving a specific purpose such as staging tables, auditing, or even individual developer workspaces. However, in other places, schemas are rarely used, and all objects are created under the default “dbo” schema.

Renaming a schema in SQL Server is not as straightforward as one might expect. There is no direct “RENAME” syntax available to change the name of a schema. However, there is a workaround that involves creating a new schema and transferring all the objects from the old schema to the new one.

The Process of Renaming a Schema

To rename a schema, you need to follow these steps:

  1. Create a new schema using the “CREATE SCHEMA” syntax.
  2. Transfer all the objects from the old schema to the new schema using the “ALTER SCHEMA” syntax with the “TRANSFER” option.
  3. Update any code or references that use the old schema name.
  4. Optionally, remove the old schema.

Example: Renaming a Schema

Let’s say we have a schema called “SallyDev” with several objects that we want to rename to a new schema called “College”. Here is an example of how you can achieve this:

CREATE SCHEMA College AUTHORIZATION dbo;

DECLARE @schema VARCHAR(100) = 'SallyDev';
DECLARE @obj VARCHAR(200);
DECLARE @cmd VARCHAR(1000);

DECLARE RenameCursor CURSOR FOR 
SELECT 
   s.name + '.' + o.name AS 'Obj'
FROM sys.objects o
INNER JOIN sys.schemas s ON s.schema_id = o.schema_id
WHERE s.name = @schema;

OPEN RenameCursor;
FETCH NEXT FROM RenameCursor INTO @obj;

WHILE @@FETCH_STATUS = 0
BEGIN
    SET @cmd = 'ALTER SCHEMA College TRANSFER ' + @obj;
    EXEC(@cmd);
    FETCH NEXT FROM RenameCursor INTO @obj;
END;

DEALLOCATE RenameCursor;

This script creates a new schema called “College” using the “CREATE SCHEMA” syntax. Then, it uses a cursor to iterate through all the objects in the “SallyDev” schema and transfers them to the “College” schema using the “ALTER SCHEMA” syntax with the “TRANSFER” option.

After running this script, all the objects from the “SallyDev” schema will be moved to the “College” schema. Remember to update any code or references that use the old schema name.

Conclusion

Although there is no direct “RENAME” syntax for schemas in SQL Server, you can achieve the same result by creating a new schema and transferring the objects. This workaround allows you to effectively rename a schema and organize your database objects in a more meaningful way.

Remember to update any code or references that use the old schema name, as the code binds to the object by name at compilation.

Using schemas can greatly improve the organization and management of your SQL Server databases. Consider utilizing schemas to group related objects and enhance the maintainability of your database systems.

Click to rate this post!
[Total: 0 Average: 0]

Let's work together

Send us a message or book free introductory meeting with us using button below.