SQL Server Scripts
- Go
back to the list of scripts.
Make the dbo the owner of all user objects
This slightly dodgy script changes all user objects that are not owned by the dbo to be owned by the dbo.
NOTE: Thanks to Stephen for making this script a little less dodgy.
-- declare the variables and the cursor
DECLARE @object_name varchar(128)
DECLARE @SQL varchar(255)
-- object UID owner:
DECLARE @object_uidname varchar(128)
-- modified query to pick up UID
DECLARE object_cursor CURSOR FOR
SELECT Sysobjects.Name as Sysobjectsname, sysusers.name as UserName
FROM SysObjects, SysUsers
WHERE SysObjects.uid = SysUsers.uid
AND sysusers.name <> 'dbo'
-- Now open the cursor and get the first object name
OPEN
object_cursor
FETCH NEXT FROM
object_cursor
INTO
@object_name, @object_uidname
-- and loop through all the object names
WHILE
@@FETCH_STATUS = 0
BEGIN
-- changing the owner of the object as we go (and printing out some guff)
PRINT 'Found owner of ' + @object_name + ' is ' + @object_uidname
SET @SQL = 'sp_ChangeObjectOwner ''' + @object_uidname + '.' + @object_name + ''', ''dbo'''
PRINT 'Executing ' + @SQL
EXEC( @SQL )
PRINT ''
-- now get for the next object name
FETCH NEXT FROM
object_cursor
INTO
@object_name, @object_uidname
-- and go back up to the while statement
END
-- finally, close the cursor
CLOSE object_cursor
-- and explicitly dump it from server scope (so we can modify the SQL query above and re-run)
DEALLOCATE object_cursor
(c)
Woric Faithfull 2003
May be freely used and distributed without permission.