当前位置:  开发笔记 > 编程语言 > 正文

重命名SQL Server架构

如何解决《重命名SQLServer架构》经验,为你挑选了4个好方法。

如何使用SQL Server重命名架构?



1> Cade Roux..:

如果模式中有大量对象,您可以使用类似的东西自动生成所有更改(它只执行表和视图,因此在运行之前,您可能需要将其扩展为SP,UDF等.)

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)



2> Ray Lu..:

您可以通过以下方式将单个对象从一个模式移动到

ALTER SCHEMA NewSchema TRANSFER OldSchema.Object;


首先,你必须使用`CREATE SCHEMA NewSchema`.
@Phil - 不是真的.如果其他人创建了'OldSchema`并且直观的想法是*重命名*它而不是创建一个新的并将对象传递给它,那么用户就不知道首先创建新的模式.
@littlegreen:您可以假设已经创建了架构,因为用户询问了如何重命名,而不是创建.

3> 小智..:

我将上面的两个代码组合在一起并使用了游标,不受字符串变量大小的限制,单独执行命令.我假设您已经创建了新架构,并且在证明一切正常后将删除旧架构.它更安全...... :)

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


我认为你在第一个'ALTER`行反转了参数的顺序.`@ NewSchema`应首先列在`ALTER SCHEMA`之后,但你首先要显示`@ OldSchema`.否则,它运作得很好.它没有深度重命名模式(sp或视图中的引用),但这些解决方案都没有.

4> KESAVAN PURU..:

存储过程重命名在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 RenameSchema 'oldname','newname'
    EXECUTE RenameSchema 'dbo','guest'  

推荐阅读
贴进你的心聆听你的世界
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有