How do I change db schema to dbo

前端 未结 11 2400
无人共我
无人共我 2020-12-02 05:38

I imported a bunch of tables from an old sql server (2000) to my 2008 database. All the imported tables are prefixed with my username, for example: jonathan.MovieData

相关标签:
11条回答
  • 2020-12-02 06:00

    Open SQL Server as SA account and click on new query past the below queries

    then click on execute, it will rollback all owned schema back to SA account

    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]
    
    0 讨论(0)
  • 2020-12-02 06:02

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

    ALTER SCHEMA schema_name TRANSFER securable_name

    0 讨论(0)
  • 2020-12-02 06:06

    You can batch change schemas of multiple database objects as described in this post:

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

    0 讨论(0)
  • 2020-12-02 06:07

    Move table from dbo schema to MySchema:

     ALTER SCHEMA MySchema TRANSFER dbo.MyTable
    


    Move table from MySchema to dbo schema:

     ALTER SCHEMA dbo TRANSFER MySchema.MyTable
    
    0 讨论(0)
  • 2020-12-02 06:08

    You can run the following, which will generate a set of ALTER sCHEMA statements for all your talbes:

    SELECT 'ALTER SCHEMA dbo TRANSFER ' + TABLE_SCHEMA + '.' + TABLE_NAME 
    FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'jonathan'
    

    You then have to copy and run the statements in query analyzer.

    Here's an older script that will do that for you, too, I think by changing the object owner. Haven't tried it on 2008, though.

    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
    

    Got it from this site.

    It also talks about doing the same for stored procs if you need to.

    0 讨论(0)
提交回复
热议问题