"The SELECT permission was denied on the object" (Msg 229 / 230)
Authentication succeeded and the object genuinely exists — this login just isn't allowed to read it. Check for an explicit DENY before assuming a missing GRANT is the whole story.
- SQL Server (object-level):
Msg 229, Level 14, State 5followed byThe SELECT permission was denied on the object 'orders', database 'shop', schema 'dbo'. - SQL Server (column-level):
Msg 230, Level 14, State 1— identical wording, but naming a specific column instead of the whole object, when a column-levelGRANT/DENYis what's in effect. - PostgreSQL:
ERROR: permission denied for table orders - MySQL:
ERROR 1142 (42000): SELECT command denied to user 'app'@'host' for table 'orders'
This is a different category from Login failed — the connection and the credentials are both fine here. This is about authorization, not authentication: the login is who it says it is, and SQL Server is saying no anyway.
Msg 229 vs. Msg 230 — the object differs, the wording doesn't
Both messages read almost identically; the distinction is entirely in
which permission actually tripped it. 229 means the whole table/view/
object was denied. 230 means a specific column was — SQL Server
supports column-level GRANT/DENY, so a login can have full access to
most of a table and be blocked from one sensitive column specifically
(a salary column on an otherwise-readable employees table, for instance).
The cause isn't always "forgot to GRANT"
The less obvious, more common-in-practice cause: an explicit DENY
exists somewhere, and DENY always wins over GRANT — including a
GRANT through group/role membership. A login can be a member of a role
that was granted SELECT, and still get Msg 229 if that same login (or
another role it belongs to) was separately denied it. Check for both
before assuming the fix is just granting access:
-- what's actually granted or denied on this object, to whom
SELECT
pr.name AS principal,
perm.permission_name,
perm.state_desc -- GRANT, DENY, or REVOKE
FROM sys.database_permissions perm
JOIN sys.database_principals pr ON pr.principal_id = perm.grantee_principal_id
WHERE perm.major_id = OBJECT_ID('dbo.orders');A DENY row here, for this login or any role it belongs to, is the real
answer — GRANTing the login directly won't override it; the DENY
has to be explicitly revoked first.
The fix, once you know which it actually is
Nothing was ever granted:
GRANT SELECT ON dbo.orders TO app_user;An explicit DENY is blocking it despite a GRANT elsewhere:
REVOKE DENY SELECT ON dbo.orders FROM app_user; -- removes the DENY
-- the existing GRANT (direct or via role) now takes effectAccess should come through role membership, not a direct grant (the usual pattern for anything beyond a handful of logins):
CREATE ROLE app_readers;
GRANT SELECT ON dbo.orders TO app_readers;
ALTER ROLE app_readers ADD MEMBER app_user;Can't demonstrate this in the playground
The playground here runs as a single implicit connection with full access
to everything — there's no second, lower-privileged login to deny
anything to, so there's nothing to reproduce this error against. Set up
GRANT/DENY against a real SQL Server instance with at least two
logins — one with full access, one deliberately restricted — to see the
actual behavior.
- Login failed for user — the layer below this one: whether the login can connect at all, before authorization is even checked.
- A network-related or instance-specific error occurred — the layer below that: whether the connection reaches the server in the first place.