SQL Server 如何修改 Schema 的名字?没办法修改

2020-08-05  本文已影响0人  HappyJoo

SQL Server 不能直接修改 schema的名字,所以只能参考这个代码,将全部转移到另一个 schema,然后删除原来要改名的 schema,然后新建schema,然后再转移回去。

DECLARE @schema VARCHAR(100) = 'SallyDev'
      , @obj VARCHAR(200)
      , @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
    SELECT @cmd = 'ALTER SCHEMA College TRANSFER ' + @obj
    EXEC(@cmd)
    FETCH NEXT FROM RenameCursor INTO @obj
END
DEALLOCATE RenameCursor
GO

参考:https://www.sqlservercentral.com/articles/renaming-a-schema-in-sql-server

上一篇下一篇

猜你喜欢

热点阅读