75

I have a table that got into the "db_owner" schema, and I need it in the "dbo" schema.

Is there a script or command to run to switch it over?

Neil N
  • 24,862
  • 16
  • 85
  • 145
DevelopingChris
  • 39,797
  • 30
  • 87
  • 118

8 Answers8

86

In SQL Server Management Studio:

  1. Right click the table and select modify (it's called "Design" now)
  2. On the properties panel choose the correct owning schema.
Restore the Data Dumps
  • 38,967
  • 12
  • 96
  • 122
  • 7
    thanks, funny, if you just right click -> properties the schema is unchangeable, so I assumed it was one of those command only ninja tasks. Glad to know that its not as painful as it felt. – DevelopingChris Sep 18 '08 at 03:12
  • 2
    Modify might be called Design depending on the version of SQL Server Management Studio being used. – cjbarth Aug 30 '11 at 21:47
  • 3
    If you only see the "Column properties" window. Go to View and choose "Properties Window". After edit, don't forget to save your changes. – Jonas Äppelgran Sep 25 '17 at 12:42
84
ALTER SCHEMA [NewSchema] TRANSFER [OldSchema].[Table1]
SqlZim
  • 37,248
  • 6
  • 41
  • 59
Stephen Wrighton
  • 36,783
  • 6
  • 67
  • 86
  • This doesnt seem to rename the table in SQL Server Management Studio from `OldSchema.Table1` to `NewSchema.Table1`. – Sanchit Jun 13 '13 at 05:48
  • 3
    Did you refresh the table list? I've found that Management Studio tends to not update the database listing to reflect changes performed via tsql (as opposed to selecting the table and using the built-in tools) unless you force it to. Or you disconnect/reconnect. – Stephen Wrighton Jun 13 '13 at 13:36
  • Yep. its definitely just a visual thing although SQL Server makes me believe that it isnt. After running your command and refreshing and whatnot, if you right click the table in design view and then change the schema from oldSchema to newSchema in the properties window then it refreshes the table name (along with a warning message). I just hope that doing that is GUI equivalent of your command and not something completely different. Is there any way to check? – Sanchit Jun 14 '13 at 00:01
12

Show all TABLE_SCHEMA by this select:

SELECT TABLE_SCHEMA, TABLE_NAME 
FROM INFORMATION_SCHEMA.TABLES 

You can use this query to change all schema for all tables to dbo table schema:

DECLARE cursore CURSOR FOR 

SELECT TABLE_SCHEMA, TABLE_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     
 FETCH NEXT FROM cursore INTO @schema, @tab     
END 

CLOSE cursore     
DEALLOCATE cursore
sth
  • 222,467
  • 53
  • 283
  • 367
7

simple answer

sp_changeobjectowner [ @objname = ] 'object' , [ @newowner = ] 'owner'

you don't need to stop all connections to the database, this can be done on the fly.

Jeremy
  • 307
  • 1
  • 6
  • 24
  • 1
    Technically, yes. It's still in Denali though and works fine for what OP was asking to do. They always say they're going to remove it, but they never do. – Jeremy Jul 29 '11 at 16:03
6

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.

Lanceomagnifico
  • 1,293
  • 15
  • 22
  • Great answer! +1 for including routines. My DB had contained them as well. There are also VIEWs and FUNCTIONs system objects that have schema field. But in my case they all were in 'dbo' schema. – Seven Jun 01 '15 at 09:36
4

When I use SQL Management Studio I do not get the 'Modify' option, only 'Design' or 'Edit'. If you have Visual Studio (I have checked VS.NET 2003, 2005 & 2008) you can use the Server Explorer to change the schema. Right click on the table and select 'Design Table' (2008) or 'Open Table Definition' (2003, 2005). Highlight the complete "Column Name" column. You can then right click and select 'Property Pages' or Properties (2008). From the property sheet you should see the 'Owner' (2003 & 2005) or 'Schema' (2008) with a drop down list for possible schemas.

Anthony K
  • 2,543
  • 4
  • 32
  • 41
2

I use this for situations where a bunch of tables need to be in a different schema, in this case the dbo schema.

declare @sql varchar(8000)
;

select
  @sql = coalesce( @sql, ';', '') + 'alter schema dbo transfer [' + s.name + '].[' + t.name + '];'
from 
  sys.tables t
  inner join
  sys.schemas s on t.[schema_id] = s.[schema_id]
where 
  s.name <> 'dbo'
;

exec( @sql )
;
Oliver
  • 668
  • 2
  • 7
  • 19
-3

You need to firstly stop all connections to the database, change the ownership of the tables that are 'db_owner' by running the command

sp_MSforeachtable @command1="sp_changeobjectowner ""?"",'dbo'"

where ? is the table name.

sth
  • 222,467
  • 53
  • 283
  • 367
Craig
  • 105
  • 1
  • 3
  • the sp_changedbowner proc is deprecated, and the sp_MSforeachtable is untrustworthy, as is sp_MSforeachdatabase .. I've taken to using Bertrand's version – Oliver Jun 08 '11 at 20:01