Welcome to

SqlServer-QA.net

Sign in | Join | Help

Show text from sql_handle

Based upon the query or stored procedure execution the plan will be stored in the cache, but it may not be in readable format as it is stored in Hexadecimal when you simply query SYSPROCESSES table. So in order to extract the query plan that is in cache, you can retrieve the SQL text of the query and the query execution plan in XML showplan format at any time.

Using the DMV sys.dm_exec_sql_text you can obtain the XML showplan and to obtain query paln you can use another DMV sys.dm_exec_query_plan dynamic management as follows:

select
(select text from sys.dm_exec_sql_text(put_sql_handle_here)) as sql_text
,(select query_plan from sys.dm_exec_query_plan(put_plan_handle_here)) as query_plan
go

Further to this Books online has another example to get average CPU time with top 5 queries:
SELECT TOP 5 total_worker_time/execution_count AS [Avg CPU Time],
    SUBSTRING(st.text, (qs.statement_start_offset/2)+1, 
        ((CASE qs.statement_end_offset
          WHEN -1 THEN DATALENGTH(st.text)
         ELSE qs.statement_end_offset
         END - qs.statement_start_offset)/2) + 1) AS statement_text
FROM sys.dm_exec_query_stats AS qs
CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) AS st
ORDER BY total_worker_time/execution_count DESC;

 

Published Thursday, October 04, 2007 3:50 AM by SQL Master

Comments

# Show text from sql_handle

Based upon the query or stored procedure execution the plan will be stored in the cache, but it may not

Thursday, October 04, 2007 3:52 AM by Other SQL Server Blogs around the Web

# Show text from sql_handle

Based upon the query or stored procedure execution the plan will be stored in the cache, but it may not

Thursday, October 04, 2007 4:24 AM by SSQA.net - SqlServer-QA.net
Anonymous comments are disabled