Changing the Owner of a MS SQL Database

By | 5 November, 2008

Have a MS SQL Database that you need to change the owner back to DBO or another user?

Try this script:

DECLARE @old sysname, @new sysname, @sql varchar(1000)

SELECT
@old = ‘old_user’
, @new = ‘new_user’
, @sql = ‘
IF EXISTS (SELECT NULL FROM INFORMATION_SCHEMA.TABLES
WHERE
QUOTENAME(TABLE_SCHEMA)+”.”+QUOTENAME(TABLE_NAME) = ”?”
AND TABLE_SCHEMA = ”’ + @old + ”’
)
EXECUTE sp_changeobjectowner ”?”, ”’ + @new + ””

EXECUTE sp_MSforeachtable @sql

Leave a Reply