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
sp_msforeachdbloops over all databases, including the system databases. Review the list before running the statements.- The
salogin may have been renamed on the instance: check its name before running the statements.