Skip to content

Instantly share code, notes, and snippets.

@duncanmcdougall
Created October 2, 2013 15:22
Show Gist options
  • Save duncanmcdougall/6795466 to your computer and use it in GitHub Desktop.
Save duncanmcdougall/6795466 to your computer and use it in GitHub Desktop.
Change all database owners on a SQL Server to another on a server in one swoop. Scary but it works.
USE master
GO
DECLARE @sqlBase nvarchar(300) = 'ALTER AUTHORIZATION ON database::XXXXX TO sa'+char(13)+char(10)
DECLARE @sql nvarchar(max)=''
SELECT @sql = @sql + REPLACE(@sqlBase,'XXXXX',name) from sys.databases
where name not in ('master', 'model', 'tempdb', 'distribution', 'msdb')
EXECUTE(@sql)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment