Category Archives: SQL Server Notes

SQL Server Change Table Owner Script

Recently I ran into a problem where all of the table I created had my account as the owner instead of DBO and I wanted a way to change the owner without doing a lot of work (I’m lazy). I also realized it would happen again so I created a script to do it for me. Here it is:

CREATE procedure ChangeTableOwnerToDBO
as

DECLARE @table nvarchar(1024)
DECLARE  mytables CURSOR FOR

SELECT NAME FROM sysobjects WHERE xtype = ‘u’ AND USER_ID() = uid AND uid != 1

OPEN mytables

FETCH NEXT FROM mytables into @table

IF @@FETCH_STATUS = -1
    BEGIN
        PRINT ‘No tables owned by you to reset!’
    END

WHILE @@FETCH_STATUS = 0
    BEGIN
        PRINT ‘Reassigning owner of ‘ + @table + ‘ to DBO’

        exec sp_changeobjectowner @table, ‘dbo’

        FETCH NEXT FROM mytables into @table
    END

CLOSE mytables
DEALLOCATE mytables

Change owner of stored procedure SQL Server

Now I am not a DBA by any means but I like to think that I am a pretty damn good Googler but it took me going to the second page of results to find out how to change the owner of a stored procedure.

A lot of posts pointed to this Microsoft article on changing the owner of MANY objects but I only wanted to change ONE.

This post is mostly for me but here is the solution:

sp_changeobjectowner ‘objectName’, ‘newObjectOwner’

It’s that simple. Now kick yourself in the ass for wasting so much time.