Sample Header Ad - 728x90

SQL Server - Finding Parent Source from T-SQL Snippet

0 votes
2 answers
128 views
The environments for which I'm responsible have some pretty substantial plan non-reuse challenges. I've run across the following query (h/t Brent Ozar blog commenter Michael J Swart) that does a fine job of itemizing the worst offenders:
WITH cte AS (
   SELECT COUNT(*) [count], query_hash, min(sql_handle) [sql_handle_example]
   FROM sys.dm_exec_query_stats
   GROUP BY query_hash
   )
SELECT cte.*, t.text [query_text_example]
FROM cte
CROSS APPLY sys.dm_exec_sql_text(sql_handle_example) t
WHERE [count] > 100
ORDER BY [count] DESC
My challenge is taking a snip of the [query_text_example] text and efficiently identifying whether it's originating from a sproc, and if so which one in which database. I've done some Googling and testing and it's been puzzlingly difficult to find a solution that takes a snippet of query text, whether it was dynamically built or not, and accurately identifies its parent sproc. Does anyone have any suggestions?
Asked by AccidentalDBA_CO (157 rep)
Apr 27, 2022, 07:02 PM
Last activity: May 1, 2022, 02:30 PM