Need to change database owner programmatically?

-- check the owner of the database
select suser_sname(owner_sid) from sys.databases where name = 'db_name'

-- change DB owner
EXEC sp_changedbowner 'sa'

-- if the first doesn't work
EXEC dbo.sp_changedbowner @loginame = 'sa', @map = false


2 Responses

  1. this script will fail because of “ ”
    also, it changes owner of the current database

    to change owner of another database run the following:
    exec [anotherdb]..sp_changedbowner ‘sa’

Leave a Reply

Your email address will not be published. Required fields are marked *