If you're encountering SQL Server error 262—*"Permission denied in database"*—it means your SQL Server authentication credentials lack the necessary permissions to access the specified database. Without fixing this, you can’t run queries, modify objects, or interact with the database at all. The fix is straightforward but requires adjusting permissions or switching to a user with the right access. Here’s how to resolve it quickly and correctly.
What You'll Need
- A SQL Server client tool (e.g., SSMS, Query Analyzer, or a third-party tool like Azure Data Studio or DBeaver)
- Administrative access to the SQL Server instance (or login with sufficient permissions to modify database roles)
- Knowledge of the database name and user attempting to access it
- The server name (e.g., `localhost` or `servername\instance`)
- Optionally, a backup of the database if you must alter permissions via SQL commands (backup first to avoid data loss)
Step-by-Step
- Connect to SQL Server with the correct credentials.
Start your SQL Server client tool and log in using either:
- Windows Authentication (if you’re on the same machine) by entering your Windows username/password, or
- SQL Server Authentication by entering a login with sysadmin or db_owner permissions on the target server.
If you’re using SSMS, go to File > Connect to Database Engine and specify the server name (e.g., `localhost\SQLEXPRESS` or a domain server). If prompted for credentials, choose Windows Authentication or SQL Server Authentication and enter valid credentials with sufficient permissions.
Tip: Labels for menus differ by version, so if a step doesn’t match your exact SSMS version, try Tools > Options > Results > Results Pane to check the exact error message for additional guidance.
- Identify the database and user causing the issue.
In SSMS, open a new query window and run:
USE [YourDatabaseName]; SELECT SUSER_SNAME() AS CurrentUser, DB_NAME() AS DatabaseName;This confirms the current user and the database they’re trying to access. If you’re unsure which user triggered the error, review recent connection attempts in the SQL Server Error Log (located in `
\Log` on the server). - Grant the user database-level permissions.
Use one of the following methods to grant permissions:
- Via SQL Server Management Studio (SSMS):
- Right-click the database in Object Explorer, select Tasks > Manage Roles.
- In the Database Roles window, find the user’s role (e.g., db_datareader, db_owner, or a custom role).
- Click Edit, then add the user to the role (e.g., by selecting the user from the Add Members dropdown).
- Click Ok to apply changes.
For a custom role, you can also use the built-in roles like db_datareader (read permissions), db_datawriter (write permissions), or db_owner (full access).
- Via T-SQL:
Open a query window and run:
USE [YourDatabaseName]; ALTER ROLE [db_datareader] ADD MEMBER [YourUsername]; -- OR for full access: ALTER ROLE [db_owner] ADD MEMBER [YourUsername];Replace `[YourDatabaseName]` and `[YourUsername]` with the actual values. For example:
USE AdventureWorks; ALTER ROLE db_datareader ADD MEMBER dbo.User1;
Safety Note: Always back up the database before altering permissions via T-SQL.
- Via SQL Server Management Studio (SSMS):
- Verify the user exists in SQL Server.
If the user doesn’t exist yet, create it with permissions. For example:
CREATE LOGIN [YourUsername] WITH PASSWORD='YourPassword'; USE [YourDatabaseName]; CREATE USER [YourUsername] FOR LOGIN [YourUsername]; ALTER ROLE db_datareader ADD MEMBER [YourUsername];Replace placeholders with your actual username, password, and database name. This creates both a login and a user, then assigns the role.
- Restart the SQL Server service (if permissions are still denied).
Sometimes, login sessions persist across restarts. To force a refresh:
- Pause the SQL Server service via Services.msc (search for it in the Start menu).
- Click Stop for SQL Server (or the instance you’re using).
- Click Start again to resume.
Alternatively, restart your PC if the issue persists.
If That Did Not Work
If you’ve granted permissions and the error still appears, check these advanced steps:- Reconnect with a sysadmin or db_owner.
If you have a login with sysadmin privileges, log in and re-run your queries. This bypasses most permission checks temporarily to confirm the issue.
- Check the SQL Server error log.
Locate the log file (typically in `
\Log\ErrorLog.txt`) and search for entries with the error code 262. This may reveal why the user was denied access (e.g., incorrect permissions, missing roles, or a blocked login). - Recreate the user login/database user.
If the user exists but permissions are misconfigured, drop and recreate it:
DROP USER [YourUsername]; USE [YourDatabaseName]; CREATE USER [YourUsername] FOR LOGIN [YourUsername]; ALTER ROLE db_datareader ADD MEMBER [YourUsername]; - Check for permissions at the SQL Server level (not just the database).
Run this query to see if the login has server-level permissions:
USE master; SELECT * FROM sys.server_principals WHERE name = 'YourUsername';If it returns nothing, the login may lack server-level access. Grant it:
USE master; ALTER ROLE [db_owner] ADD MEMBER [YourUsername];
Common Mistakes
- Using the wrong database name. Ensure the `USE` statement matches the target database and the `ALTER ROLE` command references the correct database.
- Granting permissions to the wrong user. Verify the username matches exactly (including case) when adding members to roles.
- Forgetting to restart the service. Permission changes may not take effect immediately; a service restart or PC reboot is often required.
- Using a non-existent login or user. If the user doesn’t exist, create it before assigning permissions.
- Granting permissions at the wrong level. A user with `db_datareader` (read-only) won’t work if they need writes; use `db_datawriter` or `db_owner` as needed.
Tips
- Assign the least-privileged role possible. Instead of `db_owner`, use `db_datareader` for read-only access or `db_datawriter` for write access. Avoid over-permissioning.
- Use a dedicated SQL Server user. Instead of logging in as your Windows account, create a separate login with minimal permissions for database tasks.
- Document permissions. Maintain a list of users, roles, and their assigned permissions for future reference or auditing.
- Test locally first. Before applying changes to a production database, test the fix in a staging environment.
- Avoid hardcoding passwords. Use SQL Server authentication with strong, unique passwords or SSPI (Windows Authentication) for secure connections.
Troubleshooting
If the error persists after all steps:- Check for network/firewall issues.
Ensure the SQL Server service is running and the port (`1433` by default) isn’t blocked by a firewall. Test connectivity via:
telnet [servername] 1433(Replace `[servername]` with your server’s IP or hostname.) - Verify the user exists in SQL Server.
Run:
SELECT * FROM sys.database_principals WHERE name = 'YourUsername';If no rows appear, the user doesn’t exist in the target database. - Check for database-specific restrictions.
Some databases (e.g., system databases) have additional constraints. Run:
SELECT * FROM sys.database_permissions WHERE grantee = 'YourUsername';to see if the user lacks explicit permissions. - Reinstall SQL Server.
As a last resort, reinstall SQL Server with fresh permissions. Back up your data first and restore after.
- Contact your DBA or administrator.
If you’re not the database admin, escalate the issue to the team managing SQL Server access.
Frequently Asked Questions
1. What is error 262 in SQL Server?
Error 262 occurs when a user lacks the necessary permissions to access a database or execute a query. It’s typically due to missing roles, incorrect usernames, or insufficient database-level permissions.
2. Can I fix this without being a database admin?
Yes, if you have sufficient database permissions (e.g., db_datareader, db_datawriter, or db_owner). If you’re not an admin, the DBA must grant you the necessary roles.
3. How do I grant a user permissions in SQL Server Management Studio (SSMS) without T-SQL?
Right-click the database → Tasks > Manage Roles, then add the user to an existing role like db_datareader or db_owner.
4. Why does my user still get "Permission denied" after granting roles?
Possible reasons include:
- The user doesn’t exist yet (create it first).
- The role assignment was made in the wrong database.
- The SQL Server service wasn’t restarted after changes.
- The user lacks server-level permissions (check with `sys.server_principals`).
5. How do I find out who is currently connected to the database?
Run:
SELECT * FROM sys.dm_exec_sessions WHERE session_id = @@SPID;
to see the active user, or check server logs for connection attempts.
6. Is there a way to test permissions without granting them?
Yes, log in as a user with sufficient permissions (e.g., db_owner) and attempt the query. If it fails, the issue is permission-related; if it succeeds, the problem lies elsewhere (e.g., incorrect credentials or database state).