Skip to content

Commit aa6d945

Browse files
committed
Fixing up
Because I couldn't sleep until I did
1 parent fad7335 commit aa6d945

File tree

1 file changed

+60
-62
lines changed

1 file changed

+60
-62
lines changed

sp_BlitzCache.sql

Lines changed: 60 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,6 +1066,7 @@ CREATE TABLE #configuration (
10661066
CREATE TABLE #stored_proc_info
10671067
(
10681068
SPID INT,
1069+
SqlHandle VARBINARY(64),
10691070
QueryHash BINARY(8),
10701071
variable_name NVARCHAR(128),
10711072
variable_datatype NVARCHAR(128),
@@ -2847,15 +2848,19 @@ OPTION (RECOMPILE);
28472848

28482849
IF EXISTS ( SELECT 1
28492850
FROM ##bou_BlitzCacheProcs AS bbcp
2850-
WHERE bbcp.implicit_conversions = 1
2851-
OR bbcp.QueryType LIKE 'Procedure or Function:%' )
2851+
WHERE bbcp.implicit_conversions = 1
2852+
OR bbcp.QueryType LIKE 'Procedure or Function:%')
28522853
BEGIN
28532854

28542855
RAISERROR(N'Getting information about implicit conversions and stored proc parameters', 0, 1) WITH NOWAIT;
28552856

28562857
WITH XMLNAMESPACES ( 'http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p )
28572858
, variables_types
2858-
AS ( SELECT qp.QueryHash,
2859+
AS (
2860+
2861+
--WITH XMLNAMESPACES ( 'http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p )
2862+
SELECT
2863+
qp.QueryHash,
28592864
qp.SqlHandle,
28602865
SUBSTRING(b.QueryType, CHARINDEX('[', b.QueryType), LEN(b.QueryType) - CHARINDEX('[', b.QueryType)) AS proc_name,
28612866
q.n.value('@Column', 'NVARCHAR(128)') AS variable_name,
@@ -2865,11 +2870,14 @@ AS ( SELECT qp.QueryHash,
28652870
JOIN ##bou_BlitzCacheProcs AS b
28662871
ON b.QueryHash = qp.QueryHash
28672872
CROSS APPLY qp.query_plan.nodes('//p:QueryPlan/p:ParameterList/p:ColumnReference') AS q(n)
2868-
WHERE qp.QueryHash IS NOT NULL
2869-
AND b.implicit_conversions = 1 ),
2873+
WHERE b.implicit_conversions = 1 ),
28702874
convert_implicit
2871-
AS ( SELECT qp.QueryHash,
2875+
AS (
2876+
--WITH XMLNAMESPACES ( 'http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p )
2877+
SELECT
2878+
qp.QueryHash,
28722879
qp.SqlHandle,
2880+
SUBSTRING(b.QueryType, CHARINDEX('[', b.QueryType), LEN(b.QueryType) - CHARINDEX('[', b.QueryType)) AS proc_name,
28732881
qq.c.value('@Expression', 'NVARCHAR(128)') AS expression,
28742882
SUBSTRING(
28752883
qq.c.value('@Expression', 'NVARCHAR(128)'), --Original Expression
@@ -2897,99 +2905,89 @@ AS ( SELECT qp.QueryHash,
28972905
ON b.QueryHash = qp.QueryHash
28982906
CROSS APPLY qp.query_plan.nodes('//p:QueryPlan/p:Warnings/p:PlanAffectingConvert') AS qq(c)
28992907
WHERE qq.c.exist('@ConvertIssue[.="Seek Plan"]') = 1
2900-
AND qp.QueryHash IS NOT NULL
2908+
AND qp.QueryHash IS NOT NULL
29012909
AND b.implicit_conversions = 1 )
2902-
INSERT #stored_proc_info ( SPID, QueryHash, variable_name, variable_datatype, compile_time_value, proc_name, column_name, converted_to )
2903-
SELECT @@SPID,
2904-
vt.QueryHash,
2905-
vt.variable_name,
2906-
vt.variable_datatype,
2907-
vt.compile_time_value,
2908-
vt.proc_name,
2910+
INSERT #stored_proc_info ( SPID, QueryHash, SqlHandle, variable_name, variable_datatype, compile_time_value, proc_name, column_name, converted_to )
2911+
SELECT DISTINCT
2912+
@@SPID AS SPID,
2913+
COALESCE(vt.QueryHash, ci.QueryHash) AS QueryHash,
2914+
COALESCE(vt.SqlHandle, ci.SqlHandle) AS SqlHandle,
2915+
COALESCE(vt.variable_name, ci.variable_name) AS variable_name,
2916+
COALESCE(vt.variable_datatype, ci.converted_to) AS variable_datatype,
2917+
COALESCE(vt.compile_time_value, '*declared in proc*') AS compile_time_value,
2918+
COALESCE(vt.proc_name, ci.proc_name) AS proc_name,
29092919
ci.column_name,
29102920
ci.converted_to
29112921
FROM variables_types AS vt
2912-
JOIN convert_implicit AS ci
2913-
ON ci.variable_name = vt.variable_name
2914-
AND ci.QueryHash = vt.QueryHash
2922+
RIGHT JOIN convert_implicit AS ci
2923+
ON (ci.variable_name = vt.variable_name
2924+
AND ci.QueryHash = vt.QueryHash)
29152925
OPTION(RECOMPILE);
29162926

29172927
WITH precheck AS (
29182928
SELECT spi.SPID,
2919-
spi.QueryHash,
2929+
spi.SqlHandle,
29202930
spi.proc_name,
29212931
CONVERT(XML,
2922-
'<?ClickMe -- '
2923-
+ CHAR(10)
2924-
+ 'The stored procedure '
2925-
+ spi.proc_name
2926-
+ ' had the following implicit conversions: '
2932+
N'<?ClickMe -- '
2933+
+ @nl
2934+
+ N'The '
2935+
+ CASE WHEN spi.proc_name <> 'Statement'
2936+
THEN N'stored procedure ' + spi.proc_name
2937+
ELSE N'Statement'
2938+
END
2939+
+ N' had the following implicit conversions: '
29272940
+ CHAR(10)
29282941
+ STUFF((
29292942
SELECT DISTINCT
2930-
CHAR(10)
2931-
+ 'The variable '
2943+
@nl
2944+
+ N'The variable '
29322945
+ spi2.variable_name
2933-
+ ' has a data type of '
2946+
+ N' has a data type of '
29342947
+ spi2.variable_datatype
2935-
+ ' which caused implicit conversion on the column '
2948+
+ N' which caused implicit conversion on the column '
29362949
+ spi2.column_name
2950+
+ CASE WHEN spi2.compile_time_value = '*declared in proc*'
2951+
THEN N' and is a declared variable.'
2952+
ELSE N' and is a parameter of the stored procedure.'
2953+
END
29372954
FROM #stored_proc_info AS spi2
2938-
WHERE spi.QueryHash = spi2.QueryHash
2955+
WHERE spi.SqlHandle = spi2.SqlHandle
29392956
FOR XML PATH(N''), TYPE).value(N'.[1]', N'NVARCHAR(MAX)'), 1, 1, N'')
29402957
+ CHAR(10)
2941-
+ ' -- ?>'
2958+
+ N' -- ?>'
29422959
) AS implicit_conversion_info,
29432960
CONVERT(XML,
2944-
'<?ClickMe -- '
2945-
+ CHAR(10)
2946-
+ 'This statement '
2947-
+ ' had the following implicit conversions: '
2948-
+ CHAR(10)
2949-
+ STUFF((
2950-
SELECT DISTINCT
2951-
CHAR(10)
2952-
+ 'The variable '
2953-
+ spi2.variable_name
2954-
+ ' has a data type of '
2955-
+ spi2.variable_datatype
2956-
+ ' which caused implicit conversion on the column '
2957-
+ spi2.column_name
2958-
FROM #stored_proc_info AS spi2
2959-
WHERE spi.QueryHash = spi2.QueryHash
2960-
FOR XML PATH(N''), TYPE).value(N'.[1]', N'NVARCHAR(MAX)'), 1, 1, N'')
2961-
+ CHAR(10)
2962-
+ ' -- ?>'
2963-
) AS implicit_conversion_info_statement,
2964-
CONVERT(XML,
2965-
'<?ClickMe -- '
2966-
+ CHAR(10)
2967-
+ 'EXEC '
2961+
N'<?ClickMe -- '
2962+
+ @nl
2963+
+ N'EXEC '
29682964
+ spi.proc_name
2969-
+ ' '
2965+
+ N' '
29702966
+ STUFF((
2971-
SELECT DISTINCT ', '
2967+
SELECT DISTINCT N', '
29722968
+ spi2.variable_name
2973-
+ ' = '
2969+
+ N' = '
29742970
+ CASE WHEN spi2.compile_time_value = 'NULL'
29752971
THEN spi2.compile_time_value
29762972
ELSE QUOTENAME(spi2.compile_time_value, '''')
29772973
END
29782974
FROM #stored_proc_info AS spi2
2979-
WHERE spi.QueryHash = spi2.QueryHash
2975+
WHERE spi.SqlHandle = spi2.SqlHandle
2976+
AND spi2.proc_name <> 'Statement'
2977+
AND spi2.compile_time_value <> '*declared in proc*'
29802978
FOR XML PATH(N''), TYPE).value(N'.[1]', N'NVARCHAR(MAX)'), 1, 1, N'')
2981-
+ CHAR(10)
2982-
+ ' -- ?>'
2979+
+ @nl
2980+
+ N' -- ?>'
29832981
) AS cached_execution_parameters
29842982
FROM #stored_proc_info AS spi
2985-
GROUP BY spi.SPID, spi.QueryHash, spi.proc_name
2983+
GROUP BY spi.SPID, spi.SqlHandle, spi.proc_name
29862984
)
29872985
UPDATE b
2988-
SET b.implicit_conversion_info = CASE WHEN b.QueryType = 'Statement' THEN implicit_conversion_info_statement ELSE pk.implicit_conversion_info END,
2986+
SET b.implicit_conversion_info = pk.implicit_conversion_info,
29892987
b.cached_execution_parameters = pk.cached_execution_parameters
29902988
FROM ##bou_BlitzCacheProcs AS b
29912989
JOIN precheck pk
2992-
ON pk.QueryHash = b.QueryHash
2990+
ON pk.SqlHandle = b.SqlHandle
29932991
AND pk.SPID = b.SPID
29942992
AND b.implicit_conversions = 1
29952993
OPTION(RECOMPILE);

0 commit comments

Comments
 (0)