Australian cloud for business software — built and run in Sydney since 2002

NetLeverage
NetLeverage

SQL Server error 15517: Cannot execute as the database principal because the principal does not exist

Technical article
Troubleshooting

About this article: This article is reviewed against the cited sources. This is source research, not lab validation. Check the stated versions and your own requirements before making changes.

A stored procedure, function or trigger that worked before a database was restored now fails with error 15517. Microsoft's message text reads: Cannot execute as the database principal because the principal "principal" does not exist, this type of principal can't be impersonated, or you do not have permission. When the failing statement uses EXECUTE AS OWNER, the principal named in the message is dbo, as in the message Microsoft shows in its worked example.

The message offers three explanations: the principal does not exist, this type of principal can't be impersonated, or you do not have permission. The checks below separate them without changing anything. The fix for the restore case is one statement, and it is written with the save-first step Microsoft's page does not give.

What the error means

Microsoft's page says the error "typically occurs because Microsoft SQL Server can't get the information about the execution context of the principal that's specified in a user statement or module by using the EXECUTE AS statement."

The page explains why dbo is the principal that fails. When you create a database, your login's security identifier (SID) is saved as the database owner in two places: the owner row for the database in sys.databases, and the dbo entry in sys.database_principals inside the database. Statements and modules that use EXECUTE AS OWNER "will work as expected if the SID entry that's stored for the dbo user is valid." If the SID stored for dbo does not resolve to a valid login on the server, as in the restore cases below, the module fails.

The page adds that "The issue can occur for any principal that's used in the EXECUTE AS statement and that doesn't exist on the server to which the database is restored."

Likely causes

Cause What you see How to confirm
Restore onto the same instance, but the login that created the database was removed A SQL Server login was dropped, or a Windows login is no longer valid because the person left dbo resolves to NULL in the check below
Restore onto a different server where the creating login is not valid The login does not exist there, or it exists with a different sid value (SQL Server login), or does not exist or is no longer valid (Windows login) Same check
The caller cannot impersonate the principal named in EXECUTE AS The principal exists, but the user running the module lacks permission to impersonate it The principal resolves, and the failing user lacks IMPERSONATE on it

Prove the cause before changing anything

All of these only read.

  1. Check what the database owner resolves to at server level. This is the first query on Microsoft's page, with the database name as a placeholder. SUSER_SNAME returns the login name for a SID:
SELECT NAME AS Database_Name, owner_sid, SUSER_SNAME(owner_sid) AS OwnerName
FROM sys.databases
WHERE NAME = N'YourDatabase';
  1. Check what dbo resolves to inside the database. This is the second query on Microsoft's page:
SELECT SUSER_SNAME(sid) AS Owner_Name, sid
FROM YourDatabase.sys.database_principals
WHERE NAME = N'dbo';

Save both results. In Microsoft's worked example on its page, after a restore by a different login, the first query shows the new owner and the second shows NULL for Owner_Name while the sid value is the old one. The page then shows that running the module gives the 15517 error. A NULL in the second query, with a valid name in the first, is the signature to look for.

  1. If both queries return a valid login name, dbo resolves and the third explanation may apply. The user or application that runs the module "doesn't have the necessary permissions to impersonate the principal that's specified in the EXECUTE AS statement." Microsoft's remedy for that case is to use the name of an existing principal or to grant the IMPERSONATE permission on that principal to the users who need it. The page gives no command for it, so none is given.

How to read the two results together:

First query (server level) Second query (inside the database) What it means
A valid login name NULL, with an old sid value The dbo SID stored in the database no longer resolves to a login. This is the case Microsoft's page reproduces, and the fix below is for it
A valid login name The same valid login name dbo resolves. Look at the third explanation, impersonation permission, or at another principal named in the message
Either query returns no row The database name is wrong, or you are on the wrong instance. Check the name before anything else

Microsoft's sys.databases page describes owner_sid as the "Security identifier (SID) of the external owner of the database, as registered to the server". The sid column of sys.database_principals is the SID stored inside the database. Microsoft's page says the modules work "if the SID entry that's stored for the dbo user is valid".

Example (illustrative): after moving Accounts from sql01 to sql02, the nightly import procedure fails with 15517. The first query shows the owner as sa. The second shows NULL for Owner_Name and the old SID. That matches the page's scenario for a database restored on a server where the original owner does not exist.

Fix

This is for the case where step 2 returned NULL. Microsoft's page says: "To resolve the issue that occurs because of an invalid dbo user error, change the dbo_User value to a valid login on your server".

The new owner must be a valid login. Microsoft's ALTER AUTHORIZATION page lists what can own a database: a SQL Server authentication login, a Windows authentication login representing a Windows user (not a group), or a Windows user that authenticates through a Windows authentication login representing a Windows group. Choose the login on purpose: it will own the database and the modules that run as its owner. If you are not a member of the sysadmin fixed server role, the same page says you need at least TAKE OWNERSHIP permission on the database and IMPERSONATE permission on the new owner login.

Save the current owner details first. Run the two read-only queries again, one after the other, and keep the output with the database name and the date. They are the only record of what the owner was:

SELECT NAME AS Database_Name, owner_sid, SUSER_SNAME(owner_sid) AS OwnerName
FROM sys.databases
WHERE NAME = N'YourDatabase';

SELECT SUSER_SNAME(sid) AS Owner_Name, sid
FROM YourDatabase.sys.database_principals
WHERE NAME = N'dbo';

Risk: The next statement changes who owns the database. Every module that uses EXECUTE AS OWNER will then run as the new owner, so the new owner's permissions decide what those modules can do.

Run this on the instance that holds the database, replacing the database name and the login with a valid login you have chosen on purpose:

ALTER AUTHORIZATION ON DATABASE:: YourDatabase TO [NewLogin];

To undo it: the earlier state, where dbo held a SID that resolved to no login, cannot be restored with a statement, and nothing needs restoring in the case this step is for. If the new owner turns out to be the wrong login, run the same statement again with the correct login in place of [NewLogin]. The saved output records the owner and SID values that were there before.

Confirm the fix

  • Run the second query again. Microsoft's page says the dbo entry should now resolve to the login you chose.
  • Run the module that failed. On Microsoft's page, the test procedure then "runs successfully".
  • Run the first query again. The owner name matches the login you set.
  • Compare the saved output with the new output. The sid value for dbo inside the database should now equal the owner_sid value at server level, and the Owner_Name column should show a login name instead of NULL.

What to watch afterwards

  • Other databases restored from the same source. Run the two queries for each of them. The same SID problem can affect every database restored from a source whose owner login does not exist on the new server.
  • Logins that are dropped or disabled when staff leave. Microsoft's page names a Windows login that is no longer valid because "the employee left the company" as one cause.
  • Modules that use EXECUTE AS with a named principal rather than OWNER. The page says the issue can occur for any such principal.

Risks and limits

These steps have not been run or lab-tested. Microsoft's page is dated 2023-10-18 and says "SQL Server" with no version range. It describes the restore scenario. It does not say the same fault follows an attach, so attach is not covered. Its worked example creates powerful logins to demonstrate the fault. The page gives the command for changing the owner but no save-first step or back-out, so those come from the general ALTER AUTHORIZATION page and from recording the two query results.

Sources