Cannot Execute As the Database Principal (Error 15517)

🚨Part of the SQL Server Errors series, the exact messages and what actually causes them.

Msg 15517  ·  Level 16  ·  State 1
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.
When the principal named is dbo, fix the database owner: ALTER AUTHORIZATION ON DATABASE::YourDb TO sa; That covers the overwhelmingly common case, a restored database whose owner no longer maps to a login on this instance. The message lists three possible causes in one breath; the owner is where to look first.

This error has a talent for appearing far from the thing that caused it. A procedure that ran for years starts failing after a restore. Service Broker queues go quiet. A nightly job dies on a step nobody changed. The common thread is EXECUTE AS: something asked SQL Server to run as a database principal, and SQL Server could not make that identity work.

Every scenario and fix below was reproduced on a real SQL Server 2025 instance while writing this, including the restore that manufactures the problem and the one statement that ends it.


The Message Names Three Causes. Here Is How to Tell Which One You Have

What you seeWhat it usually is
The principal named is dboThe database owner does not map to a real login. Almost always a database restored or attached from another instance: the owner travels as a SID, and this instance has no login with that SID. The classic trigger is a procedure, queue or trigger marked WITH EXECUTE AS OWNER.
The principal named is a user nameThat user genuinely does not exist in this database, or it is a type that cannot be impersonated (a role, a certificate-mapped user, a user without login in the wrong context).
The principal exists and you still get 15517Permissions. The caller needs IMPERSONATE on the target principal, and the message does not distinguish this case: measured on 2025, a login without impersonate rights asking for EXECUTE AS USER = 'dbo' is told dbo "does not exist". It exists; you cannot have it.

That third row is the trap. The wording sends you hunting for a missing principal that is not missing. If the principal obviously exists, stop reading the message as an existence claim and start reading it as a permission denial.


The Restored-Database Case, Reproduced

This is the sequence that manufactures the error from nothing, run start to finish on a real instance. A database owned by a login, carrying one procedure marked WITH EXECUTE AS OWNER. Back it up, drop it, drop the owner login, restore it. The restored copy still records the vanished login’s SID as its owner:

-- After the restore, the proc that always worked now fails:
EXEC dbo.p_ctx;
-- Msg 15517: 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.

Nothing inside the database changed. What changed is that EXECUTE AS OWNER now points at an owner SID this instance has never heard of. Confirm it in one query; a NULL owner name is the tell:

SELECT d.name, d.owner_sid, SUSER_SNAME(d.owner_sid) AS owner_login
FROM   sys.databases AS d
WHERE  SUSER_SNAME(d.owner_sid) IS NULL;

And the fix is one line. Repointing the owner to a real login made the failing procedure run again immediately, no restart, no rebuild:

ALTER AUTHORIZATION ON DATABASE::YourDb TO sa;

Whether sa or a service account is the right owner is a policy question for your estate; what matters here is that the owner maps to a login that exists on this instance. The owner login can even be disabled and the fix still holds: EXECUTE AS OWNER resolves the identity, it does not sign in, so a disabled sa owns the database and the impersonation works. Verified on SQL Server 2025.


Why You Cannot Make This Happen Deliberately, and Restores Can

Trying to reproduce this by simply dropping the owning login gets refused: Msg 15174, Login owns one or more database(s). The engine defends the front door. A restore or attach walks in through the side: ownership arrives as a SID stamped in the backup, no login check is performed, and the orphan is created silently. That is why this error clusters around migrations, DR tests and environment refreshes, and why it deserves a place on any post-restore checklist next to orphaned users.


What Not To Do

  • Do not grant your way past it with CONTROL SERVER or sysadmin. If the principal is orphaned, no grant to the caller fixes the owner mapping; you will have widened permissions and kept the error.
  • Do not recreate the procedure without EXECUTE AS to make it stop. The impersonation was presumably there for a reason, usually so callers do not need direct table rights. Removing it trades a clear error for a quieter permission problem.
  • Do not confuse this with orphaned users. Same family, different member: an orphaned user is a database user whose login is gone and has its own error and fix. Both come from the same restore, so check for both while you are there.

Common Questions

Why does it say dbo does not exist? dbo always exists.
The user dbo exists; the login it maps to does not. dbo is a mapping to the database owner’s SID, and when that SID matches no login on the instance, impersonating dbo has no identity to resolve to. Fix the owner, not the user.
The principal exists and I still get 15517. What now?
Permission. The caller needs IMPERSONATE on the target user (or the target must be in a scope the caller can impersonate). Measured on SQL Server 2025: the message for a permission failure is identical to the missing-principal one, including the words "does not exist". GRANT IMPERSONATE ON USER::target TO caller; is the narrow fix.
Which features hit this beyond EXECUTE AS in procedures?
Anything that impersonates a database principal on your behalf: Service Broker queue activation, DDL and DML triggers with EXECUTE AS, cross-database ownership chains relying on the owner mapping, and jobs that run in a database context. The restore-orphaned owner breaks all of them the same way.
Is TRUSTWORTHY involved?
Sometimes, but treat it separately. A database that relies on EXECUTE AS reaching outside itself needs either TRUSTWORTHY ON (broad, discouraged) or module signing (narrow, preferred). If your 15517 appeared after a restore, fix the owner first; TRUSTWORTHY also resets to OFF on every restore (measured on SQL Server 2025: ON before the backup, OFF after the restore) and is its own follow-up decision, not an automatic switch to flip.

Related Scripts

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *