The easiest way to set up SQL Server access is also the most dangerous: make the application connect as sa or give every account db_owner. One SQL injection bug later, the attacker owns your whole server. This guide shows the right way: give each person and application exactly the permissions it needs, and nothing more.
Four concepts you must know
| Concept | Where it lives | Purpose |
|---|---|---|
| Login | The server | The key that lets you connect to the instance at all |
| User | One database | The identity inside a database, mapped to a login |
| Role | Server or database | A named group of permissions you can assign to many users |
| Permission | On an object, schema or database | What is allowed: SELECT, INSERT, EXECUTE… |
To read data a person needs a login (to enter the server), a user in the database (to enter that database) and a permission (to touch the data).
Windows authentication or SQL authentication?
Prefer Windows (Active Directory) authentication whenever the application and the server can use it: no password is stored in a configuration file, and access ends when the account is disabled. Use SQL logins only when you must (non-Windows clients, some web hosts).
-- Windows account or group
CREATE LOGIN [CONTOSO\jsmith] FROM WINDOWS;
-- SQL login
CREATE LOGIN report_app
WITH PASSWORD = N'Use-a-long-random-passphrase-1!',
CHECK_POLICY = ON,
CHECK_EXPIRATION = OFF;
CHECK_POLICY = ON applies the Windows password complexity rules. Turning expiration off for a service account avoids an outage when the password silently expires. Rotate it on a schedule you control instead.
Step 1: Create the database user
USE SalesDB;
CREATE USER report_app FOR LOGIN report_app;
At this point the user can connect to the database but can do nothing.
Step 2: Grant only what is needed
For a reporting account, the built-in read role is enough:
ALTER ROLE db_datareader ADD MEMBER report_app;
For an application that works with one area of the database, create your own role. Say all its tables live in a schema called sales:
CREATE SCHEMA sales; -- once, if it does not exist yet
GO
CREATE ROLE app_rw;
GRANT SELECT, INSERT, UPDATE ON SCHEMA::sales TO app_rw;
GRANT EXECUTE ON SCHEMA::sales TO app_rw;
DENY DELETE ON SCHEMA::sales TO app_rw;
ALTER ROLE app_rw ADD MEMBER report_app;
Now the account can read, add and change rows and run stored procedures in sales, but cannot delete rows, change table structures or see anything outside that schema. Granting at schema level means new tables you add later are covered automatically.
Step 3: Test what the account can really do
EXECUTE AS USER = 'report_app';
SELECT * FROM fn_my_permissions(NULL, 'DATABASE');
SELECT TOP 1 * FROM sales.Orders; -- should work
DELETE FROM sales.Orders; -- should fail with a permission error
REVERT;
REVERT returns you to your own identity. Never forget it, or you keep running as the test user.
Who is powerful on your server?
SELECT m.name AS member
FROM sys.server_role_members AS rm
JOIN sys.server_principals AS r ON r.principal_id = rm.role_principal_id
JOIN sys.server_principals AS m ON m.principal_id = rm.member_principal_id
WHERE r.name = 'sysadmin';
Members of sysadmin can do anything, including reading every database and running operating system commands through some features. Keep this list as short as possible. Also lock down the built-in sa account: disable it, or at least give it a very long password you never use.
ALTER LOGIN sa DISABLE;
Orphaned users after a restore
When you restore a database to another server, its users come along but the logins they were mapped to do not exist there, so the mapping is broken (“orphaned”). Find them:
SELECT dp.name AS orphaned_user
FROM sys.database_principals AS dp
LEFT JOIN sys.server_principals AS sp ON dp.sid = sp.sid
WHERE dp.type = 'S' AND sp.sid IS NULL
AND dp.authentication_type_desc = 'INSTANCE'
AND dp.name NOT IN ('guest', 'INFORMATION_SCHEMA', 'sys');
Create the login on the new server if needed, then re-map the user:
ALTER USER report_app WITH LOGIN = report_app;
Removing access safely
Do not drop accounts straight away. First disable the login and watch for complaints; drop it later:
ALTER LOGIN report_app DISABLE;
-- later, when sure:
USE SalesDB; DROP USER report_app;
USE master; DROP LOGIN report_app;
Understanding “Login failed for user” (error 18456)
The message shown to the client is deliberately vague. The real reason is the state number in the SQL Server error log (SSMS: Management, SQL Server Logs):
| State | Meaning |
|---|---|
| 5 | The login name does not exist (check spelling and the server it points to) |
| 8 | Wrong password |
| 38 | The login is valid but the requested database cannot be opened (does not exist, offline or no access) |
The least-privilege checklist
- One account per application or person. Never share.
- Windows authentication first. Long random passwords for SQL logins.
- Permissions through roles, granted at schema level.
- No
sysadminordb_ownerfor applications. - Review who has what every few months, and disable accounts nobody uses.
More tutorials like this
Msg 208 Invalid Object Name in SQL Server: Causes and Fix
Fix SQL Server Msg 208 Invalid object name: check the spelling, the database and the schema, with real SSMS examples and scripts to find…
SQL Server Query Store Tutorial: Find Slow Queries, Compare Plans and Force a Plan
Turn on Query Store, find the most expensive queries with the sys.query_store views and the SSMS reports, compare the plans of one query and…
SPF, DKIM and DMARC Explained: How to Check and Fix Your Email DNS Records
SPF, DKIM and DMARC explained in plain language, with the exact records, dig and PowerShell commands to check them, and fixes for the most…

