How to fix Error 12402 on Windows Server 2025 / Windows 11

Troubleshooting Guide: SQL Server Error 12402 – Query ID Not Found in Query Store

Why This Error Happens

This error occurs when the query_id supplied to a Query Store stored procedure (such as sp_query_store_force_plan, sp_query_store_unforce_plan, or sp_query_store_remove_query) does not exist in the current Query Store data for the target database. This typically happens because the Query Store has been cleared or has rotated out older queries due to size limits or cleanup policies, because the query was captured in a different database than the one currently in context, or because the query_id was copied from a stale report, cached dashboard, or a different server environment entirely. It can also occur if Query Store was recently disabled and re-enabled, which resets internal ID tracking.

Step-by-Step Resolution Guide

Step 1: Verify the Correct Database Context

  • Confirm you are connected to the correct database where the query_id was originally captured, since query_id values are not unique across databases.
  • Run SELECT DB_NAME(); in SQL Server Management Studio (SSMS) to confirm the active database context.
  • If using a multi-database environment on Windows Server 2025, double-check the connection string or USE [DatabaseName]; statement at the top of your script.

Step 2: Re-Query the Query Store for a Valid Query ID

  • Run the following query to check if the query_id currently exists:
SELECT query_id, object_id, query_text_id 
FROM sys.query_store_query 
WHERE query_id = <your_query_id>;
  • If no rows are returned, the query has likely aged out of the Query Store or was never captured.
  • Retrieve a fresh, valid query_id by joining sys.query_store_query with sys.query_store_query_text to match the actual query text you intend to target.

Step 3: Check Query Store Retention and Cleanup Settings

  • Navigate to SSMS → Database Properties → Query Store page to review the “Size Based Cleanup Mode” and “Stale Query Threshold (Days)” settings.
  • If retention is set too aggressively (e.g., 1–3 days), increase the “Stale Query Threshold” value to prevent queries from being purged before you can act on them.
  • Alternatively, run this T-SQL command to adjust retention programmatically:
ALTER DATABASE [YourDatabaseName] 
SET QUERY_STORE (CLEANUP_POLICY = (STALE_QUERY_THRESHOLD_DAYS = 30));

Step 4: Confirm Query Store Is Enabled and Not Recently Reset

  • Verify Query Store is active by running:
SELECT actual_state_desc FROM sys.database_query_store_options;
  • If the state shows OFF or was recently toggled from OFF to READ_WRITE, all previous query_id values are invalidated and a new capture cycle has started.
  • Allow sufficient workload execution time for Query Store to recapture the query and generate a new, valid query_id.

Alternative or Verification Steps

  • To verify the fix, rerun your original stored procedure (e.g., sp_query_store_force_plan) using the newly retrieved query_id from Step 2 and confirm it executes without raising Error 12402.
  • As an alternative, bypass manual ID lookup entirely by using the Query Store GUI in SSMS (Query Store → Top Resource Consuming Queries) to visually select and force/unforce a plan, which automatically resolves the correct query_id for you.