CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Darrell Norton's Blog [MVP]

Fill in description here...

Change all database object owners to dbo stored proc

I hate going back and changing the owner on objects to dbo. While this stored proc is useful, I don’t like having to find all the different user names and running it for each one. So I wrote this proc to generate change owner scripts for all objects in a database not owned by dbo.

Installation

Run the sql script to create the stored proc in whatever database you choose.  Prefix it with sp_ and put it in the master database if you expect to use it across several databases on the same SQL Server instance and you don't want to duplicate it in each database (and you have permissions).

Usage

Type this in SQL Server Query Analyzer and run it:
ChangeAllObjectOwnersToDBO

In the results pane will be the text to change the owner of all non-dbo-owned objects to dbo. Copy all of the results text, paste in Query Analyzer main window, and run.  A warning message will be displayed if there is already an object of the same name owned by dbo.  You should resolve these problems on an individual basis.

You should see cautionary statements like this:
Caution: Changing any part of an object name could break scripts and stored procedures.
Don't worry, that's standard stuff. Hopefully you haven't hard-coded a specific owner! If so, I don't think this stored proc will help.

if exists (select * from sysobjects where id = object_id(N'[dbo].[ChangeAllObjectOwnersToDBO]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[ChangeAllObjectOwnersToDBO]
GO
SET QUOTED_IDENTIFIER  OFF    SET ANSI_NULLS  ON
GO

CREATE proc ChangeAllObjectOwnersToDBO
as
set nocount on

declare @uid int
declare @objName varchar(50)
declare @userName varchar(50)
declare @currObjName varchar(50)
declare @outStr varchar(256)
set @uid = user_id('dbo')

declare chObjOwnerCur cursor static
for
select user_name(uid) as 'username', [name] as 'name' from sysobjects where uid <> @uid

open chObjOwnerCur
if @@cursor_rows = 0
begin
  print 'All objects are already owned by dbo!'
  close chObjOwnerCur
  deallocate chObjOwnerCur
  return 1
end

fetch next from chObjOwnerCur into @userName, @objName
while @@fetch_status = 0
begin
  set @currObjName = 'dbo.' + @objName
  if (object_id(@currObjName) > 0)
    print 'WARNING *** ' + @currObjName + ' already exists ***'
  set @outStr = 'sp_changeobjectowner ''' + @userName + '.' + @objName + ''', ''dbo'''
  print @outStr
  print 'go'
  fetch next from chObjOwnerCur into @userName, @objName
end

close chObjOwnerCur
deallocate chObjOwnerCur
set nocount off
return 0

GO
SET QUOTED_IDENTIFIER  OFF    SET ANSI_NULLS  ON
GO



Comments

Darrell said:

Yeah, I had too!
# June 18, 2004 5:42 AM

mfDBA said:

This is great! Thanks for sharing.
# July 21, 2004 5:44 AM

Dalavai said:

This is Amazing man ,I already implemented in my production server
# August 30, 2004 12:36 AM

Enrique said:

Great post! Thanks.
# October 15, 2004 7:00 AM

Korivo said:

it seems that is doesnt rename the stored prodedure

any idea why?
# October 28, 2004 6:39 AM

Darrell said:

You run the stored proc to get a text output. Then copy the output to a NEW query analyzer window, and then run it. That should work.
# October 28, 2004 7:15 AM

Korivo said:

Great thanx a lot!

This is a great tool
# October 28, 2004 10:22 AM

Sunil said:

This is awesome. A great timesaver !!!

Thanks,
Sunil
# December 1, 2004 11:31 AM

Darrell said:

Glad you like it!
# December 1, 2004 11:43 AM

Shawn said:

Thanks for this procedure.

I have installed this on all my sql server instances.
# December 6, 2004 2:39 AM

Yusuff said:

Awesome. Thank you.
# December 7, 2004 5:12 AM

Mike said:

Saved my butt, and lots of hours of my time. Thanks!!!
# December 17, 2004 3:18 AM

Darrell said:

Enjoy!
# December 17, 2004 3:52 AM

jeff said:

Ever read MS Article #275312???
# December 31, 2004 3:09 AM

Darrell said:

Jeff - yes, actually I linked to it in my blog post and talked about what changes I made to improve it.
# December 31, 2004 11:01 AM

Jan van Veldhuizen said:

# January 12, 2005 12:19 AM

Shail said:

Great ! saved a lot of our time
# January 13, 2005 1:39 AM

Doug Sherman said:

Thanks Darrell, worked like a charm!
# February 1, 2005 9:53 AM

Brendan Tompkins said:

Yo D. Never told you how much I love this, but I've used it a bunch of times... You da man.
# February 4, 2005 1:43 PM

Darrell said:

Anytime Brendan! I'll have some more good SQL stuff here soon.
# February 4, 2005 1:49 PM

Tomas said:

Hey man, I'm having a little trouble. When I type "ChangeAllObjectOwnersToDBO" I copy the results and paste into new window. Everything is fine there, but when I run the commands, I get this:

-----------------------------------
sp_changeobjectowner 'user939811.spCCTypeFetch', 'dbo'
go
Error: No objects owned by user939811.spCCTypeF
-----------------------------------

I looks like SQL Server chops off object name: instead of "user939811.spCCTypeFetch", I only get "user939811.spCCTypeF". Of course, the script fails. I know it's not your fault, but do you know why SQL Server is behaving this way?

Thanks a lot!
Tomas
# February 8, 2005 6:09 PM

Darrell said:

Try changing the variable declarations to this:

declare @objName varchar(100)
declare @userName varchar(100)
declare @currObjName varchar(100)
declare @outStr varchar(1000)
# February 9, 2005 8:37 AM

Geoff Appleby said:

Hey Darrel,

I was needing this exact thing yesterday. My boss at work had a script that did it, but i forgot to take it home with me, and i needed to run it at home :)

So I used yours, and was happy, but i thought you might be interested in his, since it's much much much shorter. It's hard coded for dbo, but i imagine it's easy to make that a param coming into an SP instead.

Anyway, here tis.

SELECT 'EXEC(''sp_changeobjectowner @objname = '''''
+ ltrim(u.name) + '.' + ltrim(s.name)
+ ''''''
+ ', @newowner = dbo'')'
FROM
sysobjects s,
sysusers u
WHERE
s.uid = u.uid
AND u.name <> 'dbo'
AND xtype in ('V', 'P', 'U')
AND u.name not like 'INFORMATION%'
order by
s.name

He did tell me it's heavily reliant on knowing on how SQL7 and 200 work, and has no garantees it'll work in yukon :)
# March 12, 2005 3:18 AM

Darrell said:

Geoff - yep, anytime you hit sysobjects Microsoft absolves themselves of caring whether an upgrade breaks your stuff. You are supposed to use the INFORMATION_SCHEMA view (which is a SQL standard), but they don't provide access to everything under the covers! Damned if you do, damned if you don't. :)
# March 13, 2005 8:26 AM

Darrell said:

Geoff - and sweet script! It uses SQL like it's supposed to be used (set-based processing), versus creating a cursor and iterating like a programming language. I didn't ever bother to look at the script, just tweaked what was there to suit my requirements. :)
# March 13, 2005 8:28 AM

Dan Lewis's Electric Thoughts on .NET said:

Another handy dandy script: codebetter.com/.../16932.aspx

# September 28, 2007 5:48 PM
Check out Devlicio.us!