The SQL Server error 229—**"The permission was denied on the object"**—prevents users from accessing tables, views, stored procedures, or functions. This error occurs when your login account lacks the required permissions (e.g., SELECT, INSERT, EXECUTE) on the specific object. The simplest fix is to grant the missing permissions, but deeper issues (like conflicting roles or misconfigured security policies) may require additional steps—often requiring elevated permissions or administrative access.

What You'll Need

  • A SQL Server instance (local or remote)
  • Administrative or owner-level access (if modifying permissions)
  • The sp_helptext sysproc (Microsoft SQL Server 2000+) or a text editor to review stored procedures
  • SQL Server Management Studio (SSMS), Azure Data Studio, or a CLI tool like `sqlcmd`
  • An account with sufficient permissions to modify user permissions (if applicable)

Step-by-Step

  1. Identify the exact object causing the error. Run this query in SSMS or SQL Server CLI to list objects with the denied permission: SELECT OBJECT_NAME(object_id) AS ObjectName, PERMISSION_NAME AS Permission FROM sys.database_permissions WHERE grantee_principal_id = OBJECT_ID('your_login_account') AND state = 0; -- State 0 = Denied permission Replace your_login_account with the actual login (e.g., sa, domain\user). If the query fails, the account doesn’t exist in SQL Server. Check the login’s role membership: SELECT name, type_desc FROM sys.server_principals WHERE name = 'your_login_account';
  2. Grant the missing permission using GRANT. Example: If the error is on a table named Customers and you lack INSERT permissions:
    GRANT INSERT ON OBJECT::Customers TO [your_login_account]
    For views or stored procedures, use:
    GRANT SELECT ON OBJECT::ViewsName TO [your_login_account]
    For role-based access, join with:
    ALTER ROLE [YourRole] ADD MEMBER [your_login_account]
  3. Verify the permission was applied. Test with a simple query:
    USE YourDatabase;
        EXECUTE AS LOGIN = 'your_login_account';
        -- Test SELECT on the table/view
        SELECT * FROM Customers WHERE ID = 1;
        REVERT;
    If successful, the error should resolve.

If that did not work

If the error persists after granting permissions, check these advanced scenarios:
  1. Check for conflicting roles. Some roles (like db_owner) bypass individual object permissions. Exclude them:
    DENY ALL ON DATABASE::YourDatabase TO [YourRole]
    For temporary fixes, switch to a non-admin role:
    ALTER DATABASE::YourDatabase SET ENABLE_RECOVERY WITH (RECOVERY = FULL); -- Only if safe
  2. Recreate the object with the correct permission. If the object is corrupted, recreate it with explicit permissions:
    -- Backup first
        BACKUP TABLE Customers TO FILE = 'Customers.bak' WITH COMPRESSION;
    
        -- Drop and recreate with permissions
        DROP TABLE Customers;
        GO
        CREATE TABLE Customers (
            ID INT PRIMARY KEY,
            Name NVARCHAR(100)
        );
        GRANT SELECT, INSERT ON OBJECT::Customers TO [your_login_account];
        GO
    
        -- Restore
        RESTORE TABLE Customers FROM FILE = 'Customers.bak';
        GO
  3. Check for SQL Server version quirks. In SQL Server 2008+, some permissions are role-based by default. Verify with:
    EXEC sp_helprotect @objname = 'Customers', @state = 0; -- State 0 = Denied

Common Mistakes

  • Granting permissions to the sa account (unless you’re the admin). This bypasses the original user’s intended permissions. Always grant to the specific login.
  • Forgetting to test permissions. After applying changes, always verify with a test query. Use EXECUTE AS LOGIN = 'test_user' to simulate.
  • Ignoring database-level permissions. Some permissions (like CONNECT ANY DATABASE) override object-level rules. Check with:
    SELECT * FROM sys.database_permissions WHERE grantee_principal_id = OBJECT_ID('sa') AND type_desc = 'DATABASE';
  • Overwriting existing permissions. If the object has a stored procedure with the same name as a view, the query may fail. Use:
    SELECT OBJECT_NAME(object_id) FROM sys.sql_expression_dependencies
        WHERE referenced_id = OBJECT_ID('Customers');
    to avoid conflicts.

Tips

  • Use role-based access for complex permissions. Create a custom role (e.g., Data_Reader) and assign permissions to roles instead of individual users.
  • Audit permissions regularly. Run this scheduled query to check for orphaned permissions:
    SELECT p.grantee_principal_name, OBJECT_NAME(p.major_id) AS ObjectName
        FROM sys.database_permissions p
        JOIN sys.server_principals sp ON p.grantee_principal_id = sp.principal_id
        WHERE sp.is_disabled = 0 AND p.state = 0 AND NOT EXISTS (
            SELECT 1 FROM sys.database_principals rp
            WHERE rp.principal_id = p.grantee_principal_id AND rp.is_role = 1
        );
  • Document permission changes. Track who granted permissions and when via a log table in your database.
  • Use sp_addrolemember for role membership. To add a user to a role (e.g., Data_Reader):
    EXEC sp_addrolemember 'Data_Reader', 'your_login_account'
    This applies permissions granted to the role.

Troubleshooting

  1. The error persists after granting permissions—check for cached credentials. Restart SSMS or clear the credentials cache:
    -- In SSMS:
        Tools > Options > SQL Server Authentication
        Remove your login from the "Log On" list and re-enter it.
  2. You see "Permission denied on the SYS schema objects." This indicates the login lacks SYSVAR_CREATE permissions. Grant it via:
    GRANT CONTROL ON SCHEMA::SYS TO [your_login_account]
    Warning: Granting CONTROL ON SCHEMA::SYS is a security risk. Only use it for trusted users.
  3. Remote connections fail with "Permission denied." Verify network access and SQL Server’s sqlserver service is running. Check the login’s firewall settings. For Azure SQL: Ensure the login is assigned to the Database Contributor role:
    USE YourDatabase;
        ALTER ROLE DatabaseContributor ADD MEMBER [your_login_account];
  4. You’re stuck in a permission loop (e.g., "User is not assigned to the server").
    1. Switch to an admin account (e.g., sa or a user with sysadmin) and log in.
    2. Grant yourself the necessary permissions to log in.
    3. Use PowerShell to reset the login’s password if locked out:
      -- For SQL Server 2016+:
        $server = 'YourServerName';
        $password = 'NewPassword';
        Invoke-SqlCmd -ServerInstance $server -Query "
          EXEC sp_set_password @loginname = 'sa', @password = @password;
          GRANT CONNECT SQL TO [your_login_account]" -Username sa -Password $password;
      Warning: This affects the sa account. Restore the password after fixing the error.
  5. You’re using a login that doesn’t exist in SQL Server. Create the login first:
    CREATE LOGIN your_login_account WITH PASSWORD = 'SecurePassword123!';
    Then grant permissions to the new login.

Frequently Asked Questions

Why does my login work on other servers but not this one?

This usually means the login’s permissions were modified on the current server or the login doesn’t exist. Check: - The login exists on the current server (run SELECT * FROM sys.server_principals). - The login’s role membership matches the required permissions (run sp_helpuser 'your_login_account'). - The database is accessible (check CONNECT ANY DATABASE permissions).

How do I fix permissions if I don’t have admin access?

Ask the database admin to grant you the required permissions or create a custom role with minimal permissions. Example:
  1. Admin grants you Data Reader role:
  2. EXEC sp_addrolemember 'Data Reader', 'your_login_account';
  3. You now have SELECT access to all tables/views in the role.

What should I do if the sa account is locked out?

Use PowerShell to bypass the lockout (as a tech admin):
-- For SQL Server 2016+ (replace 'YourServer' with your server name)
$server = 'YourServer\InstanceName';
Invoke-SqlCmd -ServerInstance $server -Query "
    EXEC sp_grantlogin 'sa';
    ALTER ROLE db_owner ADD MEMBER sa;" -Username 'sa' -Password 'NewSecurePassword';

How do I check which permissions my login has?

Run:
USE YourDatabase;
EXECUTE AS LOGIN = 'your_login_account';
-- Test each required permission:
SELECT * FROM Customers;
REVERT;
For a full report:
SELECT
    p.name AS PermissionName,
    t.name AS TableName
FROM sys.database_permissions p
JOIN sys.tables t ON p.major_id = t.object_id
WHERE p.grantee_principal_name = 'your_login_account'
AND p.type_desc = 'OBJECT';

Is there a way to grant permissions via the Windows login?

No. SQL Server permissions are separate from Windows authentication. You must use SQL Server Authentication (login/password).

Why do I keep getting this error even after logging in as sa?

Common causes: - The login’s role membership was changed remotely (e.g., via Azure Portal). - The login exists but isn’t linked to the server (run SELECT * FROM sys.database_principals WHERE name = 'your_login_account'). - The database is in a different server or edition (e.g., Express vs. Enterprise).