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

NetLeverage
NetLeverage

SQL Server error 916: The server principal is not able to access the database under the current security context

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 user or an application connects to SQL Server without trouble, then fails the moment it touches one database. The message, in the form Microsoft documents it, is: The server principal "%.*ls" is not able to access the database "%.*ls" under the current security context. In the real message the two placeholders are replaced by the login name and the database name.

Because the login got in, this is not a sign-in failure. The login has no permission to enter that particular database. The checks below only read. Granting access comes last, and of the three options on Microsoft's page the grant to one login opens the least.

What the error means

Microsoft's page for error 916 explains the mechanism. "Logins that can connect to this instance of SQL Server but that do not have specific permissions in a database receive the permissions of the guest user." The page calls this "a security measure to prevent users in one database from connecting to other databases where they do not have privileges". The error appears "when the guest user does not have CONNECT permission to the named database", and the page adds a second case, "and the trustworthy property is not set".

A second situation is on the same page. When CONNECT permission to the msdb database is denied or revoked, SQL Server Management Studio can show this error when Object Explorer tries to show the Policy Based Management status of each database. Object Explorer uses the permissions of the current login to query msdb. A second message appears with it: "Failed to retrieve data for this request. (Microsoft.SqlServer.Management.Sdk.Sfc)".

Likely causes

Cause What you see How to confirm
The login has no user in the database and guest cannot connect One database fails; other databases work for the same login The login is not listed among the database's users
CONNECT was denied or revoked for the login or for guest Same, possibly after a security change The permission list for CONNECT in that database
CONNECT to msdb was denied or revoked Management Studio shows the error and the "Failed to retrieve data" message while expanding Object Explorer The error appears only in Object Explorer and names msdb
The database is not available to the login at all Access returns 0 for a reason other than permissions HAS_DBACCESS in the next section

Prove the cause before changing anything

All of these only read. Run them while connected as the login that fails, or as a sysadmin where noted.

  1. Ask SQL Server whether the failing login has access to the database. Microsoft's page for HAS_DBACCESS gives the form below:
SELECT HAS_DBACCESS('DatabaseName');

Replace DatabaseName with the name in the error message. Microsoft says it returns 1 if the user has access, 0 if not, and NULL if the database name is not valid. It also returns 0 "if the database is offline or suspect" and 0 "if the database is in single-user mode and the database is in use by another user". A 0 therefore does not by itself prove a permissions fault. Check the database state before you change any permission.

  1. As a sysadmin, list the users in the database and the CONNECT permissions granted or denied to them. This query follows the join Microsoft shows on its sys.database_permissions page, with a filter on the permission name:
USE DatabaseName;
SELECT pr.name, pr.type_desc, pe.permission_name, pe.state_desc
FROM sys.database_principals AS pr
INNER JOIN sys.database_permissions AS pe ON pe.grantee_principal_id = pr.principal_id
WHERE pe.permission_name = N'CONNECT';

Look for the login's user, and for guest. A state of DENY or REVOKE for either explains the error. Save this output. It is your record of the starting point.

  1. Note whether the failing user is an application login or a person. Microsoft's page warns that the options to allow access "may allow users that have permissions in one database to connect to other databases which could expose data to a malicious user", so the owner of the data should agree that this login should see this database.

Example (illustrative): at contoso.com.au a reporting login CONTOSO\svc-report was created on a new server and works on the Sales database but fails on Archive with error 916. The list in step 2 shows no user for the login in Archive and guest has no CONNECT. That matches the first row of the table.

Reading the step 2 output

Microsoft's sys.database_permissions page defines the state_desc column as one of DENY, REVOKE, GRANT or GRANT_WITH_GRANT_OPTION. Its sys.database_principals page defines type_desc for each row, for example SQL_USER, WINDOWS_USER, WINDOWS_GROUP or DATABASE_ROLE, so you can see whether the failing login has a user of its own or is covered only through a Windows group.

What the list shows What it means for the error
The login's user has CONNECT with state GRANT Permissions are not the problem. Look at the database state with HAS_DBACCESS, and at whether the failing connection really uses this login
The login's user has CONNECT with state DENY The login was explicitly blocked. Find who denied it and why before you change it
There is no user for the login, and guest has no CONNECT This is the case Microsoft's page describes: the login falls back to guest, and guest cannot connect
The row for guest shows CONNECT with state GRANT Access should already be possible through guest. Check the TRUSTWORTHY case on Microsoft's page, and confirm you are looking at the right database

If the database was restored or moved recently, also check how its users map to logins on the new server: see restored database login mapping.

Fix

Microsoft's page lists three ways to connect. They differ greatly in how much they open up, so only the first is written as a step.

The first way is to grant the specific login access to the named database. The page's own example grants a login named Adventure-Works\Larry access to the msdb database. The statement runs in the target database, and the principal named in it must be a user in that database. If step 2 showed no user for the login in this database, the statement below cannot run for it. Microsoft's error 916 page does not give a way to create the user, so for that case the steps stop here: the owner of the database has to decide whether the login should have a user in it.

Save first. Run this query in the failing database and keep its output, which shows the CONNECT permissions as they are now:

SELECT pr.name, pr.type_desc, pe.permission_name, pe.state_desc
FROM sys.database_principals AS pr
INNER JOIN sys.database_permissions AS pe ON pe.grantee_principal_id = pr.principal_id
WHERE pe.permission_name = N'CONNECT';

If the output shows a DENY for the login's user, stop here and find out who set the DENY and why. The REVOKE in the back-out below does not put a DENY back.

Risk: The next statement lets one named login connect to one database. Microsoft's page warns that "Before circumventing this security measure be sure to have a clear understanding of users are authenticated in various databases", and that when contained databases are enabled the steps "can allow database owners in one database to grant access to other database on the instance of SQL Server".

Run this in the failing database, replacing the database and the name with the user you confirmed:

USE DatabaseName;
GO
GRANT CONNECT TO [DOMAIN\UserName];

To undo it: run REVOKE CONNECT FROM [DOMAIN\UserName]; in the same database. Microsoft's REVOKE page describes REVOKE as removing permissions that were granted or denied. Then run the step 2 query again and compare it with the output you saved.

The other two options on Microsoft's page are described without steps. The first is to grant CONNECT to the guest user in the named database, which lets every login that has no user in that database in. The second is to enable the TRUSTWORTHY property on the database, using the page's ALTER DATABASE ... SET TRUSTWORTHY ON form. Microsoft's page does not give a way to reverse either, and both widen access beyond one login. If the login-specific grant does not fit, take these two to the person responsible for the database's security.

If the failure is the msdb case in Management Studio, the same first option applies: the page's example grants CONNECT on msdb to a named login.

Confirm the fix

  • Connect as the login again. The statement that failed now runs.
  • SELECT HAS_DBACCESS('DatabaseName'); returns 1 for that login.
  • In Management Studio, the "Failed to retrieve data for this request" message no longer appears when you expand the object that triggered it.

What to watch afterwards

  • Whether the same error returns for other logins after a restore or move of a database. Check database users against logins before you go live.
  • Any later DENY or REVOKE on guest or on msdb.
  • Which logins can connect to which databases. Keep a record of grants you make so they can be checked later.

Risks and limits

These steps have not been run or lab-tested. Microsoft's error page is dated 2023-02-28 and lists SQL Server without a version range. It does not say which of its descriptions of the error condition applies to a given case. The page gives no way to reverse the guest and TRUSTWORTHY options, so none is offered.

Sources