When I try to install tSQLt onto an existing database i get the following error:
The database owner SID recorded in the master database differs from
Necromaning:
If you don't want to use the SQL-Server 2000 views (deprecated), use this:
-- Restore sid when db restored from backup...
DECLARE @Command NVARCHAR(MAX)
SET @Command = N'ALTER AUTHORIZATION ON DATABASE::<> TO <>'
SELECT @Command = REPLACE
(
REPLACE(@Command, N'<>', QUOTENAME(SD.Name))
, N'<>'
,
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
Also prevents bug on oddly named database or user, and also fixes bug if no user is associated (uses sa login).