SQL Server principal “dbo” does not exist,

后端 未结 11 867
醉话见心
醉话见心 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

    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;
    

提交回复
热议问题