How do I change db schema to dbo

前端 未结 11 2484
无人共我
无人共我 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: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.

提交回复
热议问题