Change schema name on Tables and Stored procedures in SQL Server 2005

I was trying to change the 'schema' name on an object from 'username' to 'dbo' in SQL Server 2005. 
The script generates code you have to copy, paste and run on the database. 
Thanks to the DBA gurus on the sql-sqlsvr-sprocs@sqladvice.com list that provided the syntax. 
I encourage everyone looking for SQL help to check out the lists on
SQLAdvice.com http://sqladvice.com/lists/category.aspx?c=32 

Change Stored procedures in sql 2005

'Note this is the raw query
SELECT
'ALTER SCHEMA dbo TRANSFER ' + s.Name + '.' + p.Name FROM sys.Procedures p INNER JOIN
sys.Schemas s on p.schema_id = s.schema_id WHERE s.Name = 'CHANGE_ME_Username'

   It would create this kind of output.

  • ALTER SCHEMA dbo TRANSFER steveschofield.spAuthors1
  • ALTER SCHEMA dbo TRANSFER steveschofield.spAuthors2
  • ALTER SCHEMA dbo TRANSFER steveschofield.spAuthors3

You would run in a new query window, after this refresh SQL Management studio
and the stored procedures would be dbo.spAuthors1, dbo.spAuthors2, dbo.spAuthors3.

Change Table in sql 2005

Here is an article that describes the syntax, it uses the 'sp_changeobjectowner'
which isn't real "sql2005'ish" but works. 

declare @OldOwner varchar(100) declare @NewOwner varchar(100) 
set @OldOwner = 'OldOwner' set @NewOwner = 'NewOwner'

select 'sp_changeobjectowner ''[' + table_schema + '].[' +
table_name + ']'', ''' + @NewOwner + ''' go
from information_schema.tables where Table_schema = @OldOwner
Published Saturday, December 31, 2005 12:46 PM by steve schofield
Filed under:

Comments

# re: Change schema name on Tables and Stored procedures in SQL Server 2005

Tuesday, April 03, 2007 10:06 AM by Meron

it would have been more helpful it include a typical sql statement example

# re: Change schema name on Tables and Stored procedures in SQL Server 2005

Monday, May 28, 2007 12:53 PM by nix

this worked like a charm, changed tables' schemas perfectly...thanks a ton

# re: Change schema name on Tables and Stored procedures in SQL Server 2005

Monday, September 03, 2007 3:57 AM by nez

BEAUTIFUL!, thank you very much, save alot time updating a web database.

CHEERS!

# re: Change schema name on Tables and Stored procedures in SQL Server 2005

Friday, September 14, 2007 3:33 AM by Raul

It works perfectly,

Thanks a ton, but how can we change de function's schema?

# re: Change schema name on Tables and Stored procedures in SQL Server 2005

Monday, October 01, 2007 6:57 PM by jn

You can use "ALTER SCHEMA schema_name TRANSFER securable_name"

# re: Change schema name on Tables and Stored procedures in SQL Server 2005

Tuesday, October 09, 2007 5:32 AM by BAT

Thanks a lot

U have given a great solution

# re: Change schema name on Tables and Stored procedures in SQL Server 2005

Wednesday, October 10, 2007 12:43 PM by Daniel T

For tables and views you can use

SELECT 'ALTER SCHEMA NewSchema TRANSFER ' + TABLE_SCHEMA + '.' + TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'OldSchema'

# re: Change schema name on Tables and Stored procedures in SQL Server 2005

Friday, October 26, 2007 11:08 AM by Henrik

yeah verry cool. That helps me to save a lot of time.

Thanks Daniel !!

# re: Change schema name on Tables and Stored procedures in SQL Server 2005

Wednesday, October 31, 2007 7:53 PM by Michael Khalsa

Super, Great, saved lots of time changing owner for table and sprocs.

For those who do not quite understand (be sure to change sys.Procedures p

to sys.Tables p) to get your list of table change statements ready to copy from results pane to a new query window, then execute the whole list at once.

# Invalid object name ‘umbracoUser’ » Prolific Notion

Monday, February 16, 2009 4:29 PM by Invalid object name ‘umbracoUser’ » Prolific Notion

Pingback from  Invalid object name ‘umbracoUser’ » Prolific Notion