SQL Server principal “dbo” does not exist,

后端 未结 11 841
醉话见心
醉话见心 2020-12-22 16:05

I am getting the following error

Cannot execute as the database principal because the principal \"dbo\" 
does not exist, this type of principal cannot be imp         


        
相关标签:
11条回答
  • 2020-12-22 16:35

    This may also happen when the database is a restore from a different SQL server or instance. In that case, the security principal 'dbo' in the database is not the same as the security principal on the SQL server on which the db was restored. Don't ask me how I know this...

    0 讨论(0)
  • 2020-12-22 16:35

    Selected answer and some others are all good. I just want give a more SQL pure explanation. It comes to same solution that there is no (valid) database owner.

    Database owner account dbo which is mentioned in error is always created with database. So it seems strange that it doesn't exist but you can check with two selects (or one but let's keep it simple).

    SELECT [name],[sid] 
    FROM [DB_NAME].[sys].[database_principals]
    WHERE [name] = 'dbo'
    

    which shows SID of dbo user in DB_NAME database and

    SELECT [name],[sid] 
    FROM [sys].[syslogins]
    

    to show all logins (and their SIDs) for this SQL server instance. Notice it didn't write any db_name prefix, that's because every database has same information in that view.

    So in case of error above there will not be login with SID that is assigned to database dbo user.

    As explained above that usually happens when restoring database from another computer (where database and dbo user were created by different login). And you can fix it by changing ownership to existing login.

    0 讨论(0)
  • 2020-12-22 16:35

    In my case I got this error when trying to impersonate as another user. E.g.

    EXEC AS USER = 'dbo';
    

    And as the database was imported from another environment, some of its users did not match the SQL Server logins.

    You can check if you have the same problem by running the (deprecated) sp_change_users_login (in "Report" mode), or use the following query:

    select p.name,p.sid "sid in DB", (select serp.sid from sys.server_principals serp where serp.name = p.name) "sid in server"
    from sys.database_principals p
    where p.type in ('G','S','U')
    and p.authentication_type = 1
    and p.sid not in (select sid from sys.server_principals)
    

    If in that list shows the user you are trying to impersonate, then you probably can fix it by assigning the DB user to the proper login in your server. For instance:

    ALTER USER dbo WITH LOGIN = dbo;
    
    0 讨论(0)
  • 2020-12-22 16:39

    I resolved this issue by setting database owner. My database did not have had any owner before this issue. Execute this command in your database to set owner to sysadmin account:

    use [YourDatabaseName] EXEC sp_changedbowner 'sa'
    
    0 讨论(0)
  • 2020-12-22 16:44

    Do Graphically.

    Database right click-->properties-->files-->select database owner-->select [sa]-- ok

    0 讨论(0)
提交回复
热议问题