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
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’
Greetings! Very helpful advice in this particular article! It is the little changes that make the most important changes. Thanks for sharing!