How do I change db schema to dbo

前端 未结 11 2399
无人共我
无人共我 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 05:43
    USE MyDB;
    GO
    ALTER SCHEMA dbo TRANSFER jonathan.MovieData;
    GO
    

    Ref: ALTER SCHEMA

    0 讨论(0)
  • 2020-12-02 05:43

    Way to do it for an individual thing:

    alter schema dbo transfer jonathan.MovieData

    0 讨论(0)
  • 2020-12-02 05:49

    Use this code if you want to change all tables, views, and procedures at once.

    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

    It will execute a code like this

    ALTER SCHEMA dbo TRANSFER MyOldSchema.xx_GetUserInformation
    
    0 讨论(0)
  • 2020-12-02 05:55

    I just posted this to a similar question: In sql server 2005, how do I change the "schema" of a table without losing any data?


    A slight improvement to sAeid's excellent answer...

    I added an exec to have this code self-execute, and I added a union at the top so that I could change the schema of both tables AND stored procedures:

    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
    

    I too had to restore a dbdump, and found that the schema wasn't dbo - I spent hours trying to get Sql Server management studio or visual studio data transfers to alter the destination schema... I ended up just running this against the restored dump on the new server to get things the way I wanted.

    0 讨论(0)
  • 2020-12-02 05:57
    ALTER SCHEMA dbo TRANSFER jonathan.MovieData;
    

    See ALTER SCHEMA.

    Generalized Syntax:

    ALTER SCHEMA TargetSchema TRANSFER SourceSchema.TableName; 
    
    0 讨论(0)
  • 2020-12-02 05:59

    I had a similar issue but my schema had a backslash in it. In this case, include the brackets around the schema.

    ALTER SCHEMA dbo TRANSFER [DOMAIN\jonathan].MovieData;
    
    0 讨论(0)
提交回复
热议问题