Skip to content

Commit 0bea51b

Browse files
authored
Merge pull request #3863 from BrentOzarULTD/3855_sp_BlitzCache_nulls
sp_BlitzCache: fix ai_prompt returning nulls when @ai > 0
2 parents 2bba26a + eb08a06 commit 0bea51b

1 file changed

Lines changed: 23 additions & 11 deletions

File tree

sp_BlitzCache.sql

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5201,7 +5201,7 @@ BEGIN
52015201
SELECT DISTINCT DatabaseName
52025202
FROM ##BlitzCacheProcs
52035203
WHERE SPID = @@SPID
5204-
AND QueryPlan IS NOT NULL
5204+
AND (QueryPlan IS NOT NULL OR QueryText IS NOT NULL)
52055205
AND DatabaseName IS NOT NULL;
52065206

52075207
OPEN ai_db_cursor;
@@ -5330,17 +5330,16 @@ Plan Info:
53305330
Here are the warnings that popular query analysis tool sp_BlitzCache detected and suggested that we focus on - although there may be more issues, too: ' + ISNULL(Warnings, N'None') + N'
53315331
53325332
Query Text (which is cut off for long queries):
5333-
' + ISNULL(LEFT(QueryText, 4000), N'N/A') + N'
5333+
' + CASE WHEN QueryText IS NULL THEN N'(Query text could not be retrieved.)' ELSE LEFT(QueryText, 4000) END + N'
53345334
53355335
' + CASE WHEN QueryType LIKE N'Statement (parent%' THEN N' The above query is part of a batch, stored procedure, or function, so other queries may show up in the query plan. However, those other queries are irrelevant here. Focus on this specific query above, because it is one of the most resource-intensive queries in the batch. The execution plan below includes other statements in the batch, but ignore those and focus only the query above and its specific plan in the batch below. ' ELSE N' ' END + N'
53365336
53375337
XML Execution Plan:
5338-
' + ISNULL(CAST(QueryPlan AS NVARCHAR(MAX)), N'N/A') + N'
5338+
' + CASE WHEN QueryPlan IS NULL THEN N'(Query plan could not be retrieved.)' ELSE CAST(QueryPlan AS NVARCHAR(MAX)) END + N'
53395339
53405340
Thank you.'
53415341
FROM ##BlitzCacheProcs p
53425342
WHERE p.SPID = @@SPID
5343-
AND (@AI = 2 OR p.QueryPlan IS NOT NULL) /* For @AI = 1, skip null plans (no value calling AI without a plan); for @AI = 2, build prompts even when plan is null */
53445343
AND NOT (p.QueryType LIKE 'Procedure or Function:%' /* This and the below exists query makes sure that we don't get advice for parent procs, only their statements, if the statements are in our result set. */
53455344
AND EXISTS
53465345
(
@@ -5369,6 +5368,14 @@ Thank you.'
53695368
)
53705369
OPTION (RECOMPILE);
53715370

5371+
/* If both query text and query plan are null, override with a simple message - no metrics or system prompt needed */
5372+
UPDATE ##BlitzCacheProcs
5373+
SET ai_prompt = N'Prompt not generated because we can''t find the query text or query plan.'
5374+
WHERE SPID = @@SPID
5375+
AND QueryText IS NULL
5376+
AND QueryPlan IS NULL
5377+
OPTION (RECOMPILE);
5378+
53725379
IF @Debug = 2
53735380
SELECT 'After setting up ai_prompt, before calling AI' AS ai_stage, SqlHandle, QueryHash, PlanHandle, QueryPlan, ai_prompt, ai_advice, ai_raw_response
53745381
FROM ##BlitzCacheProcs
@@ -5388,11 +5395,11 @@ Thank you.'
53885395
DECLARE @AIErrorMessage NVARCHAR(4000);
53895396

53905397
DECLARE ai_cursor CURSOR LOCAL FAST_FORWARD FOR
5391-
SELECT DISTINCT SqlHandle, QueryHash, PlanHandle, ai_prompt, COALESCE(QueryType, N'') + N' - ' + LEFT(QueryText, 100)
5398+
SELECT DISTINCT SqlHandle, QueryHash, PlanHandle, ai_prompt, COALESCE(QueryType, N'') + N' - ' + LEFT(COALESCE(QueryText, N'(no text)'), 100)
53925399
FROM ##BlitzCacheProcs
53935400
WHERE SPID = @@SPID
5394-
AND QueryPlan IS NOT NULL
5395-
AND ai_prompt IS NOT NULL;
5401+
AND ai_prompt IS NOT NULL
5402+
AND (QueryPlan IS NOT NULL OR QueryText IS NOT NULL);
53965403

53975404
OPEN ai_cursor;
53985405

@@ -5531,6 +5538,7 @@ Thank you.'
55315538
SET ai_advice = N'AI prompt generated but not sent (running with @AI = 2). Review the ai_prompt column for the prompt that would be sent.'
55325539
WHERE SPID = @@SPID
55335540
AND ai_prompt IS NOT NULL
5541+
AND (QueryPlan IS NOT NULL OR QueryText IS NOT NULL)
55345542
OPTION (RECOMPILE);
55355543
END;
55365544
END;
@@ -5729,8 +5737,10 @@ BEGIN
57295737
implicit_conversion_info AS [Implicit Conversion Info],
57305738
cached_execution_parameters AS [Cached Execution Parameters], '
57315739
+ CASE WHEN @AI = 2 THEN N'
5732-
[AI Prompt] = (
5733-
SELECT (@AISystemPrompt + NCHAR(13) + NCHAR(10) + NCHAR(13) + NCHAR(10) + ai_prompt) AS [text()] FOR XML PATH(''ai_prompt''), TYPE),' ELSE N'' END
5740+
[AI Prompt] = CASE WHEN QueryText IS NULL AND QueryPlan IS NULL THEN (
5741+
SELECT ai_prompt AS [text()] FOR XML PATH(''ai_prompt''), TYPE)
5742+
ELSE (
5743+
SELECT (@AISystemPrompt + NCHAR(13) + NCHAR(10) + NCHAR(13) + NCHAR(10) + ai_prompt) AS [text()] FOR XML PATH(''ai_prompt''), TYPE) END,' ELSE N'' END
57345744
+ CASE WHEN @AI = 1 THEN N'
57355745
[AI Advice] = CASE WHEN ai_advice IS NULL THEN NULL ELSE (
57365746
SELECT ai_advice AS [text()] FOR XML PATH(''ai_advice''), TYPE) END, ' ELSE N'' END
@@ -5865,8 +5875,10 @@ BEGIN
58655875
StatementEndOffset,
58665876
PlanGenerationNum, '
58675877
+ CASE WHEN @AI <> 2 THEN N'
5868-
[AI Prompt] = (
5869-
SELECT (@AISystemPrompt + NCHAR(13) + NCHAR(10) + NCHAR(13) + NCHAR(10) + ai_prompt) AS [text()] FOR XML PATH(''ai_prompt''), TYPE),' ELSE N'' END
5878+
[AI Prompt] = CASE WHEN QueryText IS NULL AND QueryPlan IS NULL THEN (
5879+
SELECT ai_prompt AS [text()] FOR XML PATH(''ai_prompt''), TYPE)
5880+
ELSE (
5881+
SELECT (@AISystemPrompt + NCHAR(13) + NCHAR(10) + NCHAR(13) + NCHAR(10) + ai_prompt) AS [text()] FOR XML PATH(''ai_prompt''), TYPE) END,' ELSE N'' END
58705882
+ CASE WHEN @AI = 1 THEN N'
58715883
[AI Payload] = CASE WHEN ai_payload IS NULL THEN NULL ELSE (
58725884
SELECT ai_payload AS [text()] FOR XML PATH(''ai_payload''), TYPE) END,

0 commit comments

Comments
 (0)