如何将db模式更改为dbo

时间:2009-07-18 03:07:21

标签: sql sql-server database sql-server-2008 schema

我从旧的sql server(2000)导入了一堆表到我的2008数据库。所有导入的表都以我的用户名为前缀,例如:jonathan.MovieData。在表properties中,它将jonathan列为数据库模式。当我编写存储过程时,我现在必须在所有表名前包含jonathan.,这会令人困惑。

如何将所有表格更改为dbo而不是jonathan?

当前结果:jonathan.MovieData

期望的结果:dbo.MovieData

11 个答案:

答案 0 :(得分:172)

ALTER SCHEMA dbo TRANSFER jonathan.MovieData;

请参阅ALTER SCHEMA

广义语法:

ALTER SCHEMA TargetSchema TRANSFER SourceSchema.TableName; 

答案 1 :(得分:38)

您可以运行以下命令,这将为您的所有talbes生成一组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

this site获得。

如果需要,它还讨论了为存储过程做同样的事情。

答案 2 :(得分:20)

USE MyDB;
GO
ALTER SCHEMA dbo TRANSFER jonathan.MovieData;
GO

参考:ALTER SCHEMA

答案 3 :(得分:13)

将表格从dbo架构移至MySchema:

 ALTER SCHEMA MySchema TRANSFER dbo.MyTable


将表格从MySchema移至dbo架构:

 ALTER SCHEMA dbo TRANSFER MySchema.MyTable

答案 4 :(得分:8)

我刚刚发布了类似的问题:In sql server 2005, how do I change the "schema" of a table without losing any data?


对sAeid出色的答案进行了轻微改进......

我添加了一个exec让这段代码自行执行,我在顶部添加了一个union,以便我可以更改表和存储过程的模式:

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管理工作室或visual studio数据传输来改变目标架构...我最终只是运行这个反对新服务器上恢复的转储,以便按照我想要的方式获取。

答案 5 :(得分:5)

您可以批量更改多个数据库对象的模式,如本文所述:

How to change schema of all tables, views and stored procedures in MSSQL

答案 6 :(得分:2)

为个人做事的方法:

alter schema dbo transfer jonathan.MovieData

答案 7 :(得分:2)

我有类似的问题,但我的架构中有反斜杠。在这种情况下,请在架构周围添加括号。

ALTER SCHEMA dbo TRANSFER [DOMAIN\jonathan].MovieData;

答案 8 :(得分:1)

MS-帮助://MS.SQLCC.v9/MS.SQLSVR.v9.en/tsqlref9/html/0a760138-460e-410a-a3c1-d60af03bf2ed.htm

ALTER SCHEMA schema_name TRANSFER securable_name

答案 9 :(得分:1)

将SA服务器作为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]

答案 10 :(得分:0)

如果要一次更改所有表,视图和过程,请使用此代码。

BEGIN
DECLARE @name NVARCHAR(MAX)
        , @sql NVARCHAR(MAX)
        , @oldSchema NVARCHAR(MAX) = 'MyOldSchema'
        , @newSchema NVARCHAR(MAX) = 'dbo'
        
DECLARE objCursor CURSOR FOR
        SELECT  o.name
        FROM    sys.objects o
        WHERE   type IN ('P', 'U', 'V')

OPEN    objCursor
        FETCH NEXT FROM objCursor INTO @name
        WHILE   @@FETCH_STATUS = 0
        BEGIN
            SET @sql = 'ALTER SCHEMA '+ @newSchema +' TRANSFER '+ @oldSchema + '.' +  @name         
            BEGIN TRY
                exec(@sql)
                PRINT 'Success: ' + @sql
            END TRY
            BEGIN CATCH
                PRINT 'Error executing: ' + @sql
            END CATCH
            FETCH NEXT FROM objCursor INTO @name
        END
CLOSE   objCursor
DEALLOCATE  objCursor

END

它将执行这样的代码

ALTER SCHEMA dbo TRANSFER MyOldSchema.xx_GetUserInformation