Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 the database owner SID recorded in database ''. You should correct this situation by resetting the owner of database '' using the ALTER AUTHORIZATION statement.

like image 814
JDPeckham Avatar asked Sep 12 '12 13:09

JDPeckham


People also ask

How to change owner of master database in SQL Server?

Go to SQL Server Management Studio >> Right Click on the Database >> Go to Properties >> Go to Files and select OWNER.

How do I find the owner of SQL Server database?

We can view the database owner from SQL Server Management Studio. To do that, right-click on EltechDB and click on Properties. On the General screen of the Database Properties dialog box, you can see the Owner of the database in the Owner row.


2 Answers

This problem can arise when a database restored from a backup and the SID of the database owner does not match the owners SID listed in the master database. Here is a solution that uses the "ALTER AUTHORIZATION" statement recommended in the error message:

DECLARE @Command VARCHAR(MAX) = 'ALTER AUTHORIZATION ON DATABASE::[<<DatabaseName>>] TO  [<<LoginName>>]'   SELECT @Command = REPLACE(REPLACE(@Command              , '<<DatabaseName>>', SD.Name)             , '<<LoginName>>', SL.Name) FROM master..sysdatabases SD  JOIN master..syslogins SL ON  SD.SID = SL.SID WHERE  SD.Name = DB_NAME()  PRINT @Command EXEC(@Command) 
like image 174
JohnnyM Avatar answered Sep 24 '22 06:09

JohnnyM


Added this to the top of the tSQLt.class.sql script

declare @user varchar(50) SELECT  @user = quotename(SL.Name)   FROM  master..sysdatabases SD inner join master..syslogins SL     on  SD.SID = SL.SID  Where  SD.Name = DB_NAME() exec('exec sp_changedbowner ' + @user) 
like image 34
JDPeckham Avatar answered Sep 22 '22 06:09

JDPeckham