LLI Wiki — My Digital Knowledge Base

SQL Server: change the owner of all databases to sa

List the owner of each database and generate the ALTER AUTHORIZATION statements to the sa login.

List the owner of each database and generate the statements that change it to sa. The script only displays the statements: review them before running them.

List the owner of each database

SELECT suser_sname( owner_sid ), * FROM sys.databases

Generate the ALTER AUTHORIZATION statements

sp_msforeachdb 'print ''use ?  ALTER AUTHORIZATION ON DATABASE::? TO sa;'''

Notes

Mastodon LLIMastodon SocialFosstodon