Nice script
The following is missing AND is_read_only = 0
in select
INSERT INTO @LoopExecuteScripts
SELECT 'ALTER AUTHORIZATION ON DATABASE::' + name + ' TO sa'
FROM sys.databases
WHERE owner_sid <> 0x01
AND state_desc = 'ONLINE' --AND is_read_only = 0
and it will work without errors
Regards
IgorMi
Igor Micev,My blog: www.igormicev.com