如何使用 SQL Server 重命名架构?
如果模式中有大量对象,您可以使用类似的方法自动生成所有更改(它只处理表和视图,因此在运行它之前,您可能需要将其扩展到 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)
您可以通过以下方式将单个对象从一种模式移动到另一种模式:
ALTER SCHEMA NewSchema TRANSFER OldSchema.Object;
我将上面的两段代码结合起来,使用游标不受字符串变量大小的限制,单独执行命令。我假设您已经创建了新模式,并在证明一切正常后删除旧模式。这样更安全...:)
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
在 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'
这是一个简短的版本,但效果很好。
declare @sql varchar(8000), @table varchar(1000), @oldschema varchar(1000), @newschema varchar(1000)
set @oldschema = 'old'
set @newschema = 'dbo'
while exists(select * from sys.tables where schema_name(schema_id) = @oldschema)
begin
select @table = name from sys.tables
where object_id in(select min(object_id) from sys.tables where schema_name(schema_id) = @oldschema)
set @sql = 'alter schema [' + @newschema + '] transfer [' + @oldschema + '].[' + @table + ']'
exec(@sql)
end
办理手续
USE DatabaseName
DECLARE @OldSchema AS varchar(255)
DECLARE @NewSchema AS varchar(255)
SET @OldSchema = 'ComputerLearn'
SET @NewSchema = 'Basic'
DECLARE @sql AS varchar(MAX)
SET @sql = 'CREATE SCHEMA [' + @NewSchema + ']' + CHAR(13) + CHAR(10)
SELECT @sql = @sql + 'ALTER SCHEMA [' + @NewSchema + '] TRANSFER [' + sys.schemas.name + '].[' + sys.procedures.name + ']'
+ CHAR(13) + CHAR(10)
FROM sys.procedures,sys.schemas
WHERE sys.procedures.schema_id=sys.schemas.schema_id and sys.schemas.name = @OldSchema
SET @sql = @sql + 'DROP SCHEMA [' + @OldSchema + ']'
PRINT @sql
IF (0=1) EXEC (@sql)
对我有用的最简单的解决方案是:
我只有一个模式 dbo,有两个表
PopulationByCountrySTG
和 CountryRegionSTG
(1) 我通过执行创建了一个新模式,
create schema stg
(2) 我执行了以下命令,
ALTER SCHEMA stg TRANSFER dbo.PopulationByCountrySTG;
ALTER SCHEMA stg TRANSFER dbo.CountryRegionSTG;
全部完成。让我知道它是否适合你。谢谢大家。
这不是一个好的解决方案,但可能有助于了解 SQL Server 现在的工作原理。
我正在使用代码优先方法的 Entity Framework Core ORM。
我尝试使用以下步骤重命名架构:
dotnet ef database update
以上操作已完成,但架构未重命名。
我了解到我忘记删除 SQL Server 中的架构对象。 我的表正在重新创建,但我想要重命名的模式从未被删除。
一旦我删除了表并删除了架构,然后使用新架构名称运行 EF 迁移就可以了。
*(我可以这样做,因为这是一个全新的项目)