From e66af9ca6f471cd4af096cef633c483cc8681e30 Mon Sep 17 00:00:00 2001 From: Erik Darling <2136037+erikdarlingdata@users.noreply.github.com> Date: Sun, 10 May 2026 21:55:15 +0200 Subject: [PATCH] Drop redundant TRY_CONVERT(nvarchar(max), p.query_plan) wrapper sys.query_store_plan.query_plan is already nvarchar(max), so the TRY_CONVERT was a no-op identity cast. It also broke the query on databases at compatibility level <110, where the parser doesn't recognize TRY_CONVERT and reports 'nvarchar' is not a recognized built-in function name. Three call sites in QueryStoreService.cs: FetchTopPlansAsync, FetchGroupedByQueryHashAsync, and FetchGroupedByModuleAsync. Verified against a compat-100 database. Co-Authored-By: Claude Opus 4.7 (1M context) --- src/PlanViewer.Core/Services/QueryStoreService.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/PlanViewer.Core/Services/QueryStoreService.cs b/src/PlanViewer.Core/Services/QueryStoreService.cs index 7bcb496..c9158f2 100644 --- a/src/PlanViewer.Core/Services/QueryStoreService.cs +++ b/src/PlanViewer.Core/Services/QueryStoreService.cs @@ -179,8 +179,8 @@ public static async Task> FetchTopPlansAsync( // into #top_plans. Still no nvarchar(max) columns. // // Phase 4: Final SELECT — join only the TOP N winners to query_text, plan - // XML, and query metadata. Uses OUTER APPLY + TRY_CONVERT for - // safe plan XML retrieval. + // XML, and query metadata. query_plan is nvarchar(max) on the + // catalog view, so it's referenced directly without conversion. // // OPTION (RECOMPILE) on aggregation phases prevents parameter sniffing on // date range parameters producing bad plans for different time windows. @@ -299,7 +299,7 @@ FROM ranked AS r tp.query_id, tp.plan_id, qt.query_sql_text, - TRY_CONVERT(nvarchar(max), p.query_plan) AS query_plan, + p.query_plan, tp.avg_cpu_us, tp.avg_duration_us, tp.avg_reads, @@ -980,7 +980,7 @@ FROM ranked r.query_id, r.plan_id, qt.query_sql_text, -TRY_CONVERT(nvarchar(max), p.query_plan) AS plan_xml, +p.query_plan AS plan_xml, r.module_name, r.total_cpu_us, r.total_duration_us, @@ -1283,7 +1283,7 @@ FROM ranked r.query_id, r.plan_id, qt.query_sql_text, - TRY_CONVERT(nvarchar(max), p.query_plan) AS plan_xml, + p.query_plan AS plan_xml, r.total_cpu_us, r.total_duration_us, r.total_reads,