How do I change db schema to dbo

SqlSql ServerDatabaseSql Server-2008Schema

Sql Problem Overview


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. In the table properties it lists jonathan as the db schema. When I write stored procedures I now have to include jonathan. in front of all the table names which is confusing.

How do I change all my tables to be dbo instead of jonathan?

Current result: jonathan.MovieData

Desired result: dbo.MovieData

Sql Solutions


Solution 1 - Sql

ALTER SCHEMA dbo TRANSFER jonathan.MovieData;

See ALTER SCHEMA.

Generalized Syntax:

ALTER SCHEMA TargetSchema TRANSFER SourceSchema.TableName; 

Solution 2 - Sql

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.

Solution 3 - Sql

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

Ref: ALTER SCHEMA

Solution 4 - Sql

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

Solution 5 - Sql

I just posted this to a similar question: https://stackoverflow.com/questions/89606/in-sql-server-2005-how-do-i-change-the-schema-of-a-table-without-losing-any-d/10502805#10502805


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.

Solution 6 - Sql

Solution 7 - Sql

Way to do it for an individual thing:

alter schema dbo transfer jonathan.MovieData

Solution 8 - Sql

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;

Solution 9 - Sql

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]

Solution 10 - Sql

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

ALTER SCHEMA schema_name TRANSFER securable_name

Solution 11 - Sql

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionjonathan hallView Question on Stackoverflow
Solution 1 - SqlRemus RusanuView Answer on Stackoverflow
Solution 2 - SqlpatmortechView Answer on Stackoverflow
Solution 3 - SqlMitch WheatView Answer on Stackoverflow
Solution 4 - SqlNeru-JView Answer on Stackoverflow
Solution 5 - SqlLanceomagnificoView Answer on Stackoverflow
Solution 6 - Sqlanar khalilovView Answer on Stackoverflow
Solution 7 - SqlJon OnstottView Answer on Stackoverflow
Solution 8 - SqlHannover FistView Answer on Stackoverflow
Solution 9 - SqlMJ XView Answer on Stackoverflow
Solution 10 - SqlView Answer on Stackoverflow
Solution 11 - SqlRj RegaladoView Answer on Stackoverflow