Created
July 5, 2020 17:24
-
-
Save herdemu/70175e0121a7ce91d1ca3656b004294a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Q. How can i extract indivual query from it's parent? | |
A. You can extract indivual query from it's parent by using offset values. | |
When reading the script below, please note that while ISO has a zero-based offset | |
the SUBSTRING function has a one-based offset. | |
*/ | |
SELECT | |
DB_NAME(SqlText.[dbid]) AS [Database Name], | |
SqlText.text AS [Parent Query], | |
SUBSTRING | |
( | |
SqlText.text, | |
(ExecQStats.statement_start_offset/2)+1, | |
( | |
( | |
CASE | |
WHEN ExecQStats.statement_end_offset=-1 THEN LEN(CONVERT(NVARCHAR(MAX),SqlText.text)*2) | |
ELSE ExecQStats.statement_end_offset | |
END | |
) - ExecQStats.statement_start_offset | |
)/2+1 | |
) AS [Indiviual Query] | |
FROM sys.dm_exec_query_stats AS ExecQStats | |
CROSS APPLY sys.dm_exec_sql_text(ExecQStats.sql_handle) AS SqlText |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment