Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Administration (2000)
 Change owner of all objects in a DB

Author  Topic 

mr_mist
Grunnio

1870 Posts

Posted - 2003-11-10 : 03:53:53
Hi

I've recently had a database restored that has lots of objects (view, stored procedures, tables) all owned by a user that does not exist on the server. I'd like to change the owner of all the objects in the database to sa/dbo.

Is there a quick method for changing the owner of all the objects in a db? Anyone have a nifty piece of code to do it?

-------
Moo. :)

rksingh024
Yak Posting Veteran

56 Posts

Posted - 2003-11-10 : 04:02:40
This will help you...

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

Ramesh Singh
Go to Top of Page

mr_mist
Grunnio

1870 Posts

Posted - 2003-11-10 : 04:54:24
Thanks I will give it a try.

-------
Moo. :)
Go to Top of Page
   

- Advertisement -