Troubleshooting Guide: Error 8687 – Cursor Plan Failed with USE PLAN Hint
Why This Error Happens
This error occurs because the USE PLAN query hint can only be applied to cursors declared as FAST_FORWARD or STATIC. When SQL Server 2025 encounters a cursor of a different type (such as DYNAMIC, KEYSET, or default FORWARD_ONLY) combined with a forced execution plan, it cannot reconcile the cursor’s mechanics with the specified plan shape, causing the optimizer to reject the request. This typically surfaces after a plan guide, query hint, or plan forcing operation (via Query Store or sp_create_plan_guide) is applied to a stored procedure or ad-hoc query that declares a non-compliant cursor type.
Step-by-Step Resolution Guide
Step 1: Identify the Offending Query and Cursor Declaration
- Capture the exact query text using Extended Events or the SQL Server error log (filter for Error 8687).
- Locate the
DECLARE CURSORstatement in the associated stored procedure, function, or ad-hoc batch. - Check the cursor type keyword used (e.g.,
DYNAMIC,KEYSET, or no explicit type, which defaults toFORWARD_ONLY/dynamic behavior in some contexts).
Step 2: Modify the Cursor Type to a Supported Format
- Edit the T-SQL code to explicitly declare the cursor as
FAST_FORWARDorSTATIC, for example:
DECLARE cur CURSOR FAST_FORWARD FOR
SELECT column1, column2 FROM YourTable;
- If
STATICis more appropriate (e.g., the application needs a snapshot that doesn’t reflect concurrent changes), use:
DECLARE cur CURSOR STATIC FOR
SELECT column1, column2 FROM YourTable;
- Save and redeploy the modified stored procedure or script to the database.
Step 3: Remove or Replace the USE PLAN Hint
- If changing the cursor type is not feasible, remove the
OPTION (USE PLAN N'...')hint from the query. - In SQL Server Management Studio (SSMS) 21, open the query/procedure definition and strip out the
USE PLANXML hint block. - Replace it with alternative hints like
OPTION (RECOMPILE),OPTION (OPTIMIZE FOR (@param = value)), or aPLAN GUIDEtargeting a compatible query form.
Step 4: Update Statistics to Allow Natural Plan Selection
- Run
UPDATE STATISTICSon the tables referenced by the cursor query:
UPDATE STATISTICS dbo.YourTable WITH FULLSCAN;
- Alternatively, update all statistics in the database:
EXEC sp_updatestats;
- This allows the SQL Server 2025 Cardinality Estimator to choose an appropriate plan without forcing, reducing dependency on
USE PLAN.
Step 5: Validate Plan Guides and Query Store Settings
- Open SQL Server Management Studio → Databases → [YourDB] → Query Store → Top Resource Consuming Queries.
- Check if a forced plan (via Query Store “Force Plan” feature) is causing the conflict; if so, select Unforce Plan.
- If using
sp_create_plan_guidemanually, runsp_control_plan_guide 'DROP', 'GuideName'to remove conflicting guides tied to non-compliant cursors.
Alternative or Verification Steps
- After applying changes, execute the stored procedure or query directly in SSMS and confirm Error 8687 no longer appears in the Messages tab or the SQL Server Error Log (
EXEC sp_readerrorlog). - As an alternative, if forcing a specific plan is mandatory, refactor the cursor logic into a set-based operation (replacing the cursor entirely) so that
USE PLANcan be applied without cursor-type restrictions.