Troubleshooting Guide: SQL Server Error 10505 – Invalid Plan Guide Module Name
Why This Error Happens
This error occurs because the @module_or_batch parameter passed to sp_create_plan_guide requires a properly formatted two-part name consisting of schema_name.object_name when the plan guide type is set to OBJECT. SQL Server 2025 strictly validates this naming convention, and a common root cause is providing only the object name (e.g., MyProcedure) without its schema prefix, or using an incorrectly escaped/delimited string. Additionally, this can happen if the stored procedure or function referenced was created under a non-default schema, but the schema was omitted during the plan guide creation call.
Step-by-Step Resolution Guide
Step 1: Identify the Target Object and Its Schema
- Run a query against
sys.objectsandsys.schemasto confirm the exact schema of the procedure or function you are targeting. - Use the following T-SQL command to verify:
SELECT s.name AS SchemaName, o.name AS ObjectName FROM sys.objects o JOIN sys.schemas s ON o.schema_id = s.schema_id WHERE o.name = 'YourObjectName'; - Do not assume the object resides in
dbo; in SQL Server 2025 environments, objects are frequently organized under custom schemas for security partitioning.
Step 2: Correct the Two-Part Naming Syntax
- Modify your
sp_create_plan_guidescript to explicitly include the schema name followed by a period and the object name. - Ensure the value is properly enclosed in single quotes, formatted exactly as
N'schema_name.object_name'. - Example of corrected syntax:
EXEC sp_create_plan_guide @name = N'Guide1', @stmt = N'SELECT * FROM Table1', @type = N'OBJECT', @module_or_batch = N'Sales.GetOrderDetails', @params = NULL, @hints = N'OPTION (RECOMPILE)';
Step 3: Validate Database Context and Permissions
- Confirm that the current database context (
USE [YourDatabaseName];) matches the database where the schema and object actually reside. - Check that the executing user has
ALTERpermission on the schema in question, as insufficient permissions can sometimes trigger ambiguous parsing errors alongside naming issues. - Restart the query execution in SQL Server Management Studio (SSMS) after context verification to rule out cached session errors.
Step 4: Re-create the Plan Guide
- Drop the failed plan guide attempt if a partial object was created using
EXEC sp_control_plan_guide N'DROP', N'Guide1';. - Execute the corrected creation script with the validated two-part name from Step 1 and Step 2.
Alternative or Verification Steps
- To verify the fix, query the
sys.plan_guidescatalog view (SELECT * FROM sys.plan_guides WHERE name = 'Guide1';) and ensure theis_disabledcolumn returns0and thescope_type_descmatchesOBJECT. - If the error persists despite correct syntax, use
sp_help 'schema_name.object_name'to ensure there are no invisible trailing spaces or special Unicode characters in the schema or object name that are causing the two-part name parser to fail.