Cannot Execute As the Database Principal (Error 15517)

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

What Msg 15517 Says

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.

SSMS Messages tab showing Msg 15517, Level 16, State 1: cannot execute as the database principal because the principal dbo does not exist, after restoring a database whose owner login is gone

Check Which Principal SQL Server Cannot Execute As

What you seeWhat it usually is
The principal is dboThe owner SID maps to no server login on this instance. This is common after a restore or attach; modules using EXECUTE AS OWNER are affected.
A named database userThe user is absent, or is a role, certificate-mapped user, or another principal that cannot be impersonated.
The principal existsThe caller may lack IMPERSONATE. Msg 15517 does not distinguish that permission failure from a missing principal.

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.


When a Restore Leaves the Owner SID Unmapped

A restore can preserve the owner SID from the source database. If no matching server login exists on the destination instance, a procedure that uses WITH EXECUTE AS OWNER can no longer impersonate that owner.

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

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 when the owning login was dropped; the restored-database case further down is the exception, where this query comes back empty and the database is still broken:

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.

Microsoft’s reference covers EXECUTE AS and ALTER AUTHORIZATION in full.


Check the Owner SID Before Changing It

There is a catch in that check, and it is worth 30 seconds before you change anything. On this instance the restore left sys.databases.owner_sid pointing at the login that ran the RESTORE, not at the owner that vanished, so a filter on SUSER_SNAME(owner_sid) IS NULL finds nothing while the database is plainly broken. What dbo maps to inside the database is the thing that fails:

The 2 owner checks, and the fixcaptured on SQL Server 2025 CU8, read it rather than run it
Changed database context to 'master'. name|owner_login ----|----------- RestoredSales|HPAI01\Peter Changed database context to 'RestoredSales'. database_name|dbo_maps_to_login -------------|----------------- RestoredSales|NULL database_name|dbo_maps_to_login -------------|----------------- RestoredSales|sa running_as ---------- dbo

Read the first 2 results together. sys.databases says the owner resolves to a real login. sys.database_principals says the database’s own dbo, principal_id 1, maps to nothing. EXECUTE AS OWNER resolves the second, which is why the 15517 is real and the usual query is silent about it. Measured both ways round on SQL Server 2025 CU8: a restore that created the database, and a RESTORE ... WITH REPLACE over an existing one, ended up in the same state.

ALTER AUTHORIZATION resyncs the 2, and it takes effect on the next statement. The procedure that had been failing ran on the first attempt afterwards, with no restart and nothing recompiled by hand.


Why Dropping the Owner Login Is Refused

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.


Avoid Broad Permission Changes

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

Frequently Asked 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 resets to OFF when the backup is restored as a new database or after the original was dropped (measured on SQL Server 2025: ON before the backup, OFF after the restore), while a RESTORE WITH REPLACE over the same database kept it ON in the same test, so check is_trustworthy_on rather than assuming. Either way it is its own follow-up decision, not an automatic switch to flip.

Where To Go Next

If the owner mapping is correct, use these pages for related login, orphaned-user, and permission checks.

Comments

Leave a Reply

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