Link to home
Start Free TrialLog in
Avatar of Zack
ZackFlag for Australia

asked on

Find the associated stored procedure to query captured in the DMV.

Hi EE,

I have the following query that shows the status of queries running on my SQL Server.
SELECT
	r.session_id
,	r.start_time
,	TotalElapsedTime_ms = r.total_elapsed_time
,	r.[status]
,	r.command
,	DatabaseName = DB_Name(r.database_id)
,	r.wait_type
,	r.last_wait_type
,	r.wait_resource
,	r.cpu_time
,	r.reads
,	r.writes
,	r.logical_reads
,	t.[text] AS [executing batch]
,	SUBSTRING(
				t.[text], r.statement_start_offset / 2, 
				(	CASE WHEN r.statement_end_offset = -1 THEN DATALENGTH (t.[text]) 
						 ELSE r.statement_end_offset 
					END - r.statement_start_offset ) / 2 
			 ) AS [executing statement] 
,	p.query_plan
FROM
	sys.dm_exec_requests r
CROSS APPLY
	sys.dm_exec_sql_text(r.sql_handle) AS t
CROSS APPLY	
	sys.dm_exec_query_plan(r.plan_handle) AS p
ORDER BY 
	r.total_elapsed_time DESC;

Open in new window


Under executing batch I get the following text:

(@P1 varchar(11),@P2 varchar(1))select  distinct o.order_id, o.enddate, lb.value lookback, hb.billinv_id hasbilling   from ordcalc oc   inner join ordcalc pri on (pri.order_id = @P1 )   inner join orders prio on (pri.order_id = prio.order_id)   inner join orders o on (oc.order_id = o.order_id and prio.client_id = o.client_id)   inner join funddept fd on (o.funder_id = fd.funder_id and o.dept_id = fd.dept_id and fd.invformat = 'COMBOSTAT')   inner join funderrefs fr on (o.funder_id = fr.funder_id and fr.name = 'Combine Invoice Code' and fr.textval = isnull(@P2, fr.textval))   outer apply (select XMLData.value('data(/PropBag/Prop[@Name = "Lookback Period"]/@Value)[1]', 'varchar(200)') value   from (   select convert(xml, REFDATA) as XmlData from funderrefs   where NAME = 'Combo Statement Settings'   and funder_id = o.funder_id   ) as t1) as lb   outer apply (select top 1 billinv_id from billinv bi where   o.client_id=bi.client_id and o.order_id=bi.order_id and bi.invoutamt > 0 ) as hb   

Open in new window


For the parameters @P1 and @P2 how do I determine what stored procedure this came from?

Any assistance is appreciated.

Thank you.
ASKER CERTIFIED SOLUTION
Avatar of Raja Jegan R
Raja Jegan R
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yes, Raja is correct. But this query will show just currently executed commands.

You should rather check the overall resources consumption Better than to develop your own tools you may download e.g. this Firat Response kit from Brent Ozar: https://www.brentozar.com/first-aid/
Avatar of Zack

ASKER

Hi Guys,

Thanks for the information I found a post on extended events that works really well to capture parameter values as well:

https://sqlperformance.com/2019/02/extended-events/capture-queries-sql-server
>> I found a post on extended events that works really well to capture parameter values as well

Just FYI, Profiler trace was there in SQL Server for a really long time and Extended events started off in SQL Server 2008..
In other words, Extended events was an advanced or improvised version of Profiler trace with advantages and disadvantages in both approaches..