VIEW SERVER STATE permission was denied (Msg 300): a least-privilege diagnostics login
By Scott Coffey, Senior Database Developer. Updated 2026-11-10.
You hand a monitoring tool, a consultant, or an AI coding agent a login, and the first wait stats query comes back with this:
Msg 300, Level 14, State 1, Line 1
VIEW SERVER PERFORMANCE STATE permission was denied on object 'server', database 'master'.
Msg 297, Level 16, State 1, Line 1
The user does not have permission to perform this action.
On SQL Server 2019 and older the message names VIEW SERVER STATE. On SQL Server 2022 and later it usually names VIEW SERVER PERFORMANCE STATE, one of the more granular permissions added in that release. Either way, the login can connect but cannot read server-scoped dynamic management views such as sys.dm_os_wait_stats or sys.dm_exec_query_stats.
The common "fix" is adding the login to sysadmin. That works, and it also lets the login drop databases, change configuration, and read every table. A diagnostics login needs to see state, not change it.
Step 1: know which DMVs fail quietly
Not every DMV raises Msg 300. In a test on SQL Server 2022, a login with no extra permissions got the error from sys.dm_os_wait_stats, but sys.dm_exec_requests and sys.dm_exec_sessions returned without error and showed only the login's own session. A blocking query run under that login reports "no blocking" because it cannot see the other sessions, not because there is none.
So an empty result from a diagnostic script is not proof of a healthy server until you know the login has the right permissions.
Step 2: check what the login can see
Run this as the login you are testing. It is read-only:
SELECT HAS_PERMS_BY_NAME(NULL, NULL, N'VIEW SERVER STATE') AS view_server_state,
HAS_PERMS_BY_NAME(NULL, NULL, N'VIEW SERVER PERFORMANCE STATE') AS view_server_perf_state, -- 2022+
HAS_PERMS_BY_NAME(NULL, NULL, N'VIEW ANY DEFINITION') AS view_any_definition;
SELECT permission_name
FROM sys.fn_my_permissions(NULL, N'SERVER')
ORDER BY permission_name;
On SQL Server 2019 and older, leave out the VIEW SERVER PERFORMANCE STATE line; the permission does not exist there. On 2022, a login granted VIEW SERVER STATE also returns 1 for VIEW SERVER PERFORMANCE STATE and VIEW SERVER SECURITY STATE, because the older permission covers both of the newer ones.
Step 3: grant view permissions, not a role
Create a dedicated login for diagnostics. Do not reuse a person's login or an application login. This script uses a sqlcmd variable for the password so no secret is stored in the file; run it with sqlcmd -v DiagPassword="..." and review it before you do:
USE master;
CREATE LOGIN diag_reader WITH PASSWORD = N'$(DiagPassword)', CHECK_POLICY = ON;
-- SQL Server 2022+: the narrower permission is enough for wait stats,
-- sessions, requests, blocking, plan cache, and index usage DMVs.
GRANT VIEW SERVER PERFORMANCE STATE TO diag_reader;
-- SQL Server 2019 and older: use this line instead.
-- GRANT VIEW SERVER STATE TO diag_reader;
GRANT VIEW ANY DEFINITION TO diag_reader; -- read object definitions, not data
GRANT CONNECT ANY DATABASE TO diag_reader; -- open each database, read no tables
What each grant does, as tested on SQL Server 2022:
VIEW SERVER PERFORMANCE STATEwas enough forsys.dm_os_wait_stats, all sessions insys.dm_exec_sessions,sys.dm_exec_input_bufferfor other sessions, andsys.dm_exec_query_statswithsys.dm_exec_sql_text.VIEW ANY DEFINITIONletOBJECT_DEFINITIONreturn procedure source in a user database.CONNECT ANY DATABASElet the login open a user database and readsys.dm_db_partition_statsrow counts, whileSELECTon a user table still failed with Msg 229. It sees the shape of the data, not the data.
None of these lets the login change anything. Leave out VIEW ANY DEFINITION if the procedure source itself is sensitive. If you only need wait stats and blocking, the first grant alone may be enough.
Step 4: give it Agent job history
Agent job data lives in msdb, and the server-level grants above do not cover it. A natural guess is SQLAgentReaderRole. That role lets the login see jobs in SQL Server Management Studio and through msdb.dbo.sp_help_job, but in testing it did not allow a direct SELECT on msdb.dbo.sysjobs or sysjobhistory; both failed with Msg 229. Most job failure queries read those tables directly, so grant SELECT on the specific tables:
USE msdb;
CREATE USER diag_reader FOR LOGIN diag_reader;
ALTER ROLE SQLAgentReaderRole ADD MEMBER diag_reader; -- SSMS Agent node, sp_help_job
GRANT SELECT ON dbo.sysjobs TO diag_reader;
GRANT SELECT ON dbo.sysjobsteps TO diag_reader;
GRANT SELECT ON dbo.sysjobhistory TO diag_reader;
GRANT SELECT ON dbo.sysjobschedules TO diag_reader;
GRANT SELECT ON dbo.syscategories TO diag_reader;
GRANT EXECUTE ON dbo.agent_datetime TO diag_reader;
An UPDATE on msdb.dbo.sysjobs from this login still fails with Msg 229. It can read job history, as the queries in the Agent job failure guide do, and cannot start, stop, or edit jobs.
Step 5: test with the real scripts, then trim
Connect as the new login and run the diagnostics it exists for. In this test, the SQL Rig wait-stats-delta.sql, blocking-chain.sql, expensive-queries.sql, and agent-job-inventory.sql files all ran under this login without a permission error. Check row counts too: a blocking query that returns only one session means a grant is still missing, as Step 1 showed.
Then review the grants once a quarter, like any other access:
SELECT pr.name, pe.permission_name, pe.state_desc
FROM sys.server_permissions AS pe
JOIN sys.server_principals AS pr ON pr.principal_id = pe.grantee_principal_id
WHERE pr.name = N'diag_reader'
ORDER BY pe.permission_name;
Why this matters more with AI agents
An AI coding agent connected with a sysadmin login can run anything it decides to run. Connected with this login, the worst a mistaken query can do is read state. The SQL Rig permission-least-privilege.sql file applies the same idea inside a database (roles, EXECUTE on schemas, explicit DENY on writes) with a dry run by default, and the pack's FORBIDDEN-PATTERNS.md lists adding a login to sysadmin "to make it work" as a pattern to refuse, in favor of the minimum right that succeeds. See also guardrails for AI coding agents on SQL Server.