Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rename SQL Server Schema

People also ask

How do I change the default schema name in SQL Server?

In that case, just use SQL Server Management Studio > Database > Security > Users > Properties and change the default schema there.

Can you rename a schema?

You must own the schema to use ALTER SCHEMA . To rename a schema you must also have the CREATE privilege for the database. To alter the owner, you must also be a direct or indirect member of the new owning role, and you must have the CREATE privilege for the database.


If you have a large number of objects in a schema, you can use something like this to generate all the changes automatically (it only does tables and views, so before you run it, you might need to expand it to SPs, UDFs, etc.)

USE SandBox

DECLARE @OldSchema AS varchar(255)
DECLARE @NewSchema AS varchar(255)
DECLARE @newLine AS varchar(2) = CHAR(13) + CHAR(10)

SET @OldSchema = 'dbo'
SET @NewSchema = 'StackOverflow'

DECLARE @sql AS varchar(MAX)

SET @sql = 'CREATE SCHEMA [' + @NewSchema + ']' + @newLine
SELECT @sql = @sql + 'GO' + @newLine
SELECT @sql = @sql + 'ALTER SCHEMA [' + @NewSchema + '] TRANSFER [' + TABLE_SCHEMA + '].[' + TABLE_NAME + ']'
     + @newLine
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = @OldSchema

SET @sql = @sql + 'DROP SCHEMA [' + @OldSchema + ']'

PRINT @sql -- NOTE PRINT HAS AN 8000 byte limit - 8000 varchar/4000 nvarchar - see comments
IF (0=1) EXEC (@sql)

You move individual objects from one schema to another via:

ALTER SCHEMA NewSchema TRANSFER OldSchema.Object;

I have combined both codes above and used cursors to not be limited by the size of the string variables, executing the commands individually. I assume you have already created the new schema and will drop the old one after certifying all is ok. It's safer... :)

DECLARE @OldSchema AS varchar(255)
DECLARE @NewSchema AS varchar(255)

SET @OldSchema = 'dbo'
SET @NewSchema = 'StackOverflow'

DECLARE @sql AS varchar(MAX)

DECLARE @Schema AS varchar(MAX)
DECLARE @Obj AS varchar(MAX)

-- First transfer Tables and Views

DECLARE CU_OBJS CURSOR FOR
    SELECT TABLE_SCHEMA, TABLE_NAME
    FROM INFORMATION_SCHEMA.TABLES
    WHERE TABLE_SCHEMA = @OldSchema

OPEN CU_OBJS

FETCH NEXT FROM CU_OBJS
INTO @Schema, @Obj

WHILE @@FETCH_STATUS = 0
BEGIN
    SELECT @sql = 'ALTER SCHEMA [' + @NewSchema + '] TRANSFER [' + @OldSchema + '].[' + @Obj + ']'
    PRINT @sql
--  EXEC (@sql)

    FETCH NEXT FROM CU_OBJS
    INTO @Schema, @Obj
END

CLOSE CU_OBJS
DEALLOCATE CU_OBJS


-- Now transfer Stored Procedures

DECLARE CU_OBJS CURSOR FOR
    SELECT sys.schemas.name, sys.procedures.name
    FROM sys.procedures,sys.schemas
    WHERE sys.procedures.schema_id=sys.schemas.schema_id and sys.schemas.name = @OldSchema

OPEN CU_OBJS

FETCH NEXT FROM CU_OBJS
INTO @Schema, @Obj

WHILE @@FETCH_STATUS = 0
BEGIN
    SELECT @sql = 'ALTER SCHEMA [' + @NewSchema + '] TRANSFER [' + @Schema + '].[' + @Obj + ']'
    PRINT @sql
--  EXEC (@sql)

    FETCH NEXT FROM CU_OBJS
    INTO @Schema, @Obj
END

CLOSE CU_OBJS
DEALLOCATE CU_OBJS

The stored procedure to rename the schema which has more tables in SQL server 2008

   IF OBJECT_ID ( 'dbo.RenameSchema', 'P' ) IS NOT NULL 
    DROP PROCEDURE dbo.RenameSchema;
   GO                         

CREATE PROCEDURE dbo.RenameSchema               

 @OLDNAME  varchar(500),
@NEWNAME  varchar(500)

AS            
     /*check for oldschema exist or not */
     IF NOT EXISTS (SELECT 1 FROM sys.schemas WHERE name =  @OLDNAME)

        BEGIN

            RETURN

        END

       /* Create the schema with new name */
      IF NOT EXISTS (SELECT 1 FROM sys.schemas WHERE name = @NEWNAME)

      BEGIN

          EXECUTE( 'CREATE SCHEMA ' + @NEWNAME );

       END                

     /* get the object under the old schema and transfer those objects to new schema */
     DECLARE Schema_Cursor CURSOR FOR

    SELECT ' ALTER SCHEMA ' + @NEWNAME + ' TRANSFER '+ SCHEMA_NAME(SCHEMA_ID)+'.'+ name  
    as ALTSQL from sys.objects WHERE type IN ('U','V','P','Fn') AND 
    SCHEMA_NAME(SCHEMA_ID) = @OLDNAME;

   OPEN Schema_Cursor;          

  DECLARE @SQL varchar(500)         

   FETCH NEXT FROM Schema_Cursor INTO @SQL;

   WHILE @@FETCH_STATUS = 0
    BEGIN
    exec (@SQL) 
    FETCH NEXT FROM Schema_Cursor INTO @SQL;
   END;

   CLOSE Schema_Cursor;

   DEALLOCATE Schema_Cursor;

   /* drop the old schema which should be the user schema */
   IF @OLDNAME <> 'dbo' and  @OLDNAME <> 'guest'
   BEGIN
    EXECUTE ('DROP SCHEMA ' + @OLDNAME) 
    END
   GO

Execute the procedure to rename the schema: examples:

    EXECUTE RenameSchema 'oldname','newname'
    EXECUTE RenameSchema 'dbo','guest'