Answers:
ALTER SCHEMA dbo TRANSFER jonathan.MovieData;
请参阅ALTER SCHEMA。
通用语法:
ALTER SCHEMA TargetSchema TRANSFER SourceSchema.TableName;
[DOMAIN\user].[tableName]
。通常,在SQL Server社区中,使用Transact-SQL引用的标识符([
和]
),"
除非有特殊要求,否则应避免使用。
[
and ]
,就像您想精确了解SQL对象名称的其他情况一样。[domain\user123].TableName
。
您可以运行以下命令,这将为您的所有表生成一组ALTER sCHEMA语句:
SELECT 'ALTER SCHEMA dbo TRANSFER ' + TABLE_SCHEMA + '.' + TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'jonathan'
然后,您必须在查询分析器中复制并运行语句。
我认为这是一个较旧的脚本,它将通过为您更改对象所有者来为您完成此操作。不过,在2008年还没有尝试过。
DECLARE @old sysname, @new sysname, @sql varchar(1000)
SELECT
@old = 'jonathan'
, @new = 'dbo'
, @sql = '
IF EXISTS (SELECT NULL FROM INFORMATION_SCHEMA.TABLES
WHERE
QUOTENAME(TABLE_SCHEMA)+''.''+QUOTENAME(TABLE_NAME) = ''?''
AND TABLE_SCHEMA = ''' + @old + '''
)
EXECUTE sp_changeobjectowner ''?'', ''' + @new + ''''
EXECUTE sp_MSforeachtable @sql
从此站点得到它。
如果需要,它还讨论了对存储的proc进行相同的操作。
sys.tables
(sys.objects的子集)。
USE MyDB;
GO
ALTER SCHEMA dbo TRANSFER jonathan.MovieData;
GO
参考:ALTER SCHEMA
我刚刚将其发布到一个类似的问题:在sql server 2005中,如何更改表的“模式”而不丢失任何数据?
一个微小的改进赛义德的出色答卷......
我添加了一个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数据传输以更改目标架构...我最终只是针对还原后的数据库运行了该数据库转储到新服务器上,以我想要的方式进行操作。
您可以按照本文中的说明批量更改多个数据库对象的架构:
打开SQL Server作为SA帐户,然后单击以下查询之后的新查询
然后单击执行,它将所有拥有的架构回滚到SA帐户
alter authorization on schema::[db_datareader] to [dbo]
alter authorization on schema::[db_datareader] to [db_datareader]
alter authorization on schema::[db_datawriter] to [dbo]
alter authorization on schema::[db_datawriter] to [db_datawriter]
alter authorization on schema::[db_securityadmin] to [dbo]
alter authorization on schema::[db_securityadmin] to [db_securityadmin]
alter authorization on schema::[db_accessadmin] to [dbo]
alter authorization on schema::[db_accessadmin] to [db_accessadmin]
alter authorization on schema::[db_backupoperator] to [dbo]
alter authorization on schema::[db_backupoperator] to [db_backupoperator]
alter authorization on schema::[db_ddladmin] to [dbo]
alter authorization on schema::[db_ddladmin] to [db_ddladmin]
alter authorization on schema::[db_owner] to [dbo]
alter authorization on schema::[db_owner] to [db_owner]
ms-help://MS.SQLCC.v9/MS.SQLSVR.v9.en/tsqlref9/html/0a760138-460e-410a-a3c1-d60af03bf2ed.htm
ALTER SCHEMA schema_name传输securable_name