我有一个进入“ db_owner”模式的表,并且在“ dbo”模式中需要它。
是否有运行脚本或命令以进行切换?
Answers:
在SQL Server Management Studio中:
ALTER SCHEMA [NewSchema] TRANSFER [OldSchema].[Table1]
OldSchema.Table1
为NewSchema.Table1
。
TABLE_SCHEMA
通过此选择显示全部:
SELECT TABLE_SCHEMA, TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
您可以使用此查询将所有表的所有架构更改为dbo表架构:
DECLARE cursore CURSOR FOR
SELECT TABLE_SCHEMA, TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA <> 'dbo'
DECLARE @schema sysname,
@tab sysname,
@sql varchar(500)
OPEN cursore
FETCH NEXT FROM cursore INTO @schema, @tab
WHILE @@FETCH_STATUS = 0
BEGIN
SET @sql = 'ALTER SCHEMA dbo TRANSFER ' + @schema + '.' + @tab
PRINT @sql
FETCH NEXT FROM cursore INTO @schema, @tab
END
CLOSE cursore
DEALLOCATE cursore
PRINT @sql
应将其更改为EXEC(@sql)
一个微小的改进赛义德的出色答卷......
我添加了一个exec以使该代码自动执行,并且在顶部添加了一个并集,以便可以更改两个表和存储过程的架构:
DECLARE cursore CURSOR FOR
select specific_schema as 'schema', specific_name AS 'name'
FROM INFORMATION_SCHEMA.routines
WHERE specific_schema <> 'dbo'
UNION ALL
SELECT TABLE_SCHEMA AS 'schema', TABLE_NAME AS 'name'
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA <> 'dbo'
DECLARE @schema sysname,
@tab sysname,
@sql varchar(500)
OPEN cursore
FETCH NEXT FROM cursore INTO @schema, @tab
WHILE @@FETCH_STATUS = 0
BEGIN
SET @sql = 'ALTER SCHEMA dbo TRANSFER [' + @schema + '].[' + @tab +']'
PRINT @sql
exec (@sql)
FETCH NEXT FROM cursore INTO @schema, @tab
END
CLOSE cursore
DEALLOCATE cursore
我也必须还原dbdump,发现该架构不是dbo-我花了数小时试图获取Sql Server Management Studio或Visual Studio数据传输来更改目标架构...我最终只是针对还原的数据库运行转储到新服务器上,以所需的方式获取内容。