Necromaning:
se non si desidera utilizzare le viste SQL-Server 2000 (deprecate), utilizzare questa:
DECLARE @Command NVARCHAR(MAX)
SET @Command = N'ALTER AUTHORIZATION ON DATABASE::<<DatabaseName>> TO <<LoginName>>'
SELECT @Command = REPLACE
(
REPLACE(@Command, N'<<DatabaseName>>', QUOTENAME(SD.Name))
, N'<<LoginName>>'
,
QUOTENAME
(
COALESCE
(
SL.name
,(SELECT TOP 1 name FROM sys.server_principals WHERE type_desc = 'SQL_LOGIN' AND is_disabled = 'false' ORDER BY principal_id ASC )
)
)
)
FROM sys.databases AS SD
LEFT JOIN sys.server_principals AS SL
ON SL.SID = SD.owner_sid
WHERE SD.Name = DB_NAME()
PRINT @command
EXECUTE(@command)
GO
Previene anche bug su database o utenti con nomi strani e corregge anche bug se nessun utente è associato (usa login sa).