312

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 impersonated,
or you do not have permission.

I read about ALTER AUTHORIZATION, but I have no idea what database this is happening in. This error is getting spit out very frequently, and grows the error log by about 1GB every day.

PBG
  • 8,944
  • 7
  • 34
  • 48
  • 1
    This is probably a question for the DBA site, but it would help if you give more information about when the error appears, i.e. what command cannot be executed. And there are plenty of search results for this error including [this question](http://stackoverflow.com/questions/2723061/sql-server-2008-replication-failing-with-process-could-not-execute-sp-replcmds); have you looked at them and do they match your situation and configuration in any way? – Pondlife Dec 11 '12 at 16:31

12 Answers12

662

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'
Hogan
  • 7,224
  • 2
  • 22
  • 15
  • 6
    See detailed article here: http://sqlserver-help.com/tag/cannot-execute-as-the-database-principal-because-the-principal/ – orberkov Jul 27 '14 at 06:26
  • 9
    @hurleystylee, your solution actually worked well for me. My DB had an owner btw. – Saturn K Feb 24 '16 at 00:37
  • I am having this same issue. I have tried to run the query by @hurleystylee it executed but it did not do anything. When I checked `dbo` was still the db_owner and I CANNOT DO ANYTHING TO DBO. It's getting really frustrating. I can't change a thing. – Wairimu Murigi Aug 25 '17 at 09:44
  • @hurleystylee please consider editing and complementing the answer so that people won't have to look at the comments to find out the command syntax. – Ulysses Alves Mar 05 '18 at 17:08
  • @UlyssesAlves thanks! Back when I made that comment, I didn't know I could edit the answer. Looks like the OP already did it, though. – hurleystylee Mar 06 '18 at 14:18
  • 2
    @hurleystylee yes, I see he did. I think this way the answer becomes more complete on its own. – Ulysses Alves Mar 06 '18 at 14:27
  • Great, this works for me, I have windows authentication so I put the name of the user. I found it in Security - Logins. – amc software Dec 24 '19 at 23:22
  • Hmm not helping, error message after running sp_changedbowner: "The proposed new database owner is already a user or aliased in the database." – Motlicek Petr Jan 04 '20 at 10:47
  • worked for me in the case of restoring a backup from a totally different server with different users, etc – Mike M Jan 09 '20 at 15:26
  • After 7 years it is still valid – Pimenta Mar 12 '20 at 14:15
  • Worked for me in first try. Saved lot of time. Just implemented on username instead of sa. Great TYVM. – Yeshwant Mudholkar Apr 17 '20 at 03:57
  • On my first google hit from a Microsoft page, it suggested to use "a valid user name", but trying my own account name or 'dbo' did not work, but 'sa' did. – PMF Apr 17 '20 at 06:23
  • This does work for me, changing the db_owner from "domain\user" to sa, but are there any concerns about making this change? – mrc Nov 30 '20 at 12:28
  • Worked for me after unsuccesfully trying to access a database backup from another machine. – Ladrillo Apr 06 '22 at 16:20
168

enter image description here

Do Graphically.

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

Vishe
  • 3,383
  • 1
  • 24
  • 23
41
USE [<dbname>]
GO
sp_changedbowner '<user>' -- you can use 'sa' as a quick fix in databases with SQL authentication

KB913423 - You cannot run a statement or a module that includes the EXECUTE AS clause after you restore a database in SQL Server 2005

moudrick
  • 2,148
  • 1
  • 22
  • 34
user3779273
  • 411
  • 4
  • 2
35

After restoring a Database from SQL2016 to SQL2019, I had the same issue when I try to access Database Diagrams. I had the correct Database owner already but owner of Files was empty. Once I set that, it worked properly...

enter image description here

TheNightOwl
  • 528
  • 5
  • 6
  • 1
    This turned out to be exactly my issue. In fact, I changed the owner to 'sa' instead of the owner I wanted and it STILL worked. Thank you for this. – D-Klotz Feb 15 '21 at 17:14
  • Thanks, it worked for me, I like it more to write it down the Owner on This Menu instead of running T-SQL ALTER.... A Question: Before I made this change: If you see properties for the DataBase it was a owner for Database Owner, but if i you see Owner in Files there wasn't, ¿Why is this differences? Regards from México – JWBG Jul 30 '21 at 14:19
14

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

Peter Huppertz
  • 361
  • 3
  • 7
  • Can I ask you how to resolve it? lol, that's exactly what I'm trying to do. Move database diagrams between different servers and then implement the database. I got this error once I imported the .bak file and tried to open the diagrams folder. – gunslingor Nov 18 '16 at 01:49
  • 2
    Hey, this worked for me: http://dba.stackexchange.com/questions/50690/error-on-changing-ownership-of-database-from-files-page-in-database-properties-d – ironstone13 Nov 21 '16 at 12:46
  • @ironstone13 didn't work for me. I got the message that I cannot drop dbo – Wairimu Murigi Aug 25 '17 at 09:59
12

another way of doing it

ALTER AUTHORIZATION 
ON DATABASE::[DatabaseName]
TO [A Suitable Login];
Mugiwara
  • 866
  • 1
  • 11
  • 33
  • its working for Azure SQL Server while the accepted answer is working for SQL 2012 to SQL 2022 and throw error --could not find stored procedure 'sp_changedbowner'-- on Azure SQL Server – Chinh Phan Aug 23 '22 at 08:31
7

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.

Bizniztime
  • 1,016
  • 10
  • 22
0

Under Security, add the principal as a "SQL user without login", make it own the schema with the same name as the principal and then in Membership make it db_owner.

0

Also had this error when accidentally fed a database connection string to the readonly mirror - not the primary database in a HA setup.

fiat
  • 15,501
  • 9
  • 81
  • 103
0

As the message said, you should set permission as owner to your user. So you can use following:

ALTER AUTHORIZATION 
ON DATABASE::[YourDBName]
TO [UserLogin];

Hope helpful! Leave comment if it's ok for you.

0

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;
Mariano Desanze
  • 7,847
  • 7
  • 46
  • 67
-1

Go to the Properties - Files. The owner name must be blank. Just put "sa" in the user name and the issue will be resolved.