The database owner SID recorded in the master database differs from the database owner SID recorded in database 'AdventureWorks2008R2'. You should correct this situation by resetting the owner of database 'AdventureWorks2008R2' using the ALTER AUTHORIZATION statement.

--To get owner SID recorded in the master database for the current database
SELECT owner_sid FROM sys.databases WHERE database_id=DB_ID()

--To get the owner SID recorded for the current database owner
SELECT sid FROM sys.database_principals WHERE name=N'dbo'

They should return you same SID values in the format of a GUID.

Now if the two SID's differ which they did in my case it means that you need to reset the database owner so that both values are the same. To do this you can run another ALTER statement and pass in the owner value you want to use e.g

use AdventureWorks2008R2

ALTER AUTHORIZATION ON Database::AdventureWorks2008R2 TO sa

or

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)

The problem will be fixed.

Comments