The database owner SID recorded in the master database differs from the database owner SID

前端 未结 5 1776
时光说笑
时光说笑 2020-12-12 14:23

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

5条回答
  •  醉话见心
    2020-12-12 15:01

    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).

提交回复
热议问题