Search
Home
Articles
Backup
Books
Certification
FAQ
Products
Replication
Scripts
Seminars
Training
TSQL

MSDN Fourms
Philippine SSUG
Fort Worth SSUG
Oklahoma City SSDG

Resume

MHS Enterprises
BlowFrog Software
FilAm Software
AcrylicAcetate.com
Bargain Humidors
Western Humidor

How can I change the owner of an object?

With SQL 7.0 there is a stored-procedure (sp_changeobjectowner) to do this, however under SQL 6.5 and earlier there is no supported method to do this. (With 7.0 the sp doesn't work for user-defined types so you'll have to use the direct method).

It can be achieved by directly addressing and updating the system table concerned though.
  1. Get the uid number for the new owner from sysusers
    ( select name, uid from sysusers where name = "<New Owner Name>" )
  2. Configure your server to allow updates to system tables
    ( exec sp_configure "allow updates",1 )
    ( reconfigure with override )
  3. Begin a transaction
  4. Update the sysobjects table, changing the uid column value of the objects concerned to the uid you want.
    ( update sysobjects set uid = <New UID> where name = "<Table Name>" )
  5. Check that the right number of rows have been affected.
  6. Commit or rollback the transaction, depending on the result
  7. Configure your server to NOT allow updates to system tables
    ( exec sp_configure "allow updates",0 )
    ( reconfigure with override )
  8. Stop and start your SQL Server

(The last step is necessary as portions of system tables are kept in memory by SQL Server, and the only way to force these to update is to recycle SQL Server)

as an example :-

select * from sysobjects where uid = user_id('user1')
exec sp_configure 'allow updates', '1'
reconfigure WITH OVERRIDE
go

BEGIN TRANSACTION
update systypes set uid = user_id('dbo') where uid = user_id('user1')
COMMIT TRANSACTION

exec sp_configure 'allow updates', '0'
reconfigure WITH OVERRIDE
go

Michael R. Hotek

All content on this site, except where noted, represents an original work of Michael R. Hotek and is protected by applicable copyright laws. The SQL Server FAQ is the sole work of Neil Pike. No page, portion of a page, or download may be used for commercial purposes in whole or in part without the express, written permission of the applicable author.