How to get parameter values for dm_exec_sql_text

Edit : Remus is correct, this will only bring out the compiled versions on the first time that the query plan hit the cache, not subsequent runs.

You should be able to get the parameters from the query plan, since it contains the last parameters used. Altering your code:

select * 
from sys.dm_exec_requests r 
cross apply sys.dm_exec_query_plan(plan_handle) as qp
cross apply sys.dm_exec_sql_text(r.sql_handle) 
where r.database_id = DB_ID('<dbname>') 

You will find the final column of the query plan is query_plan, an xml version of the query plan which you can manually inspect, at the bottom of the XML are the parameters, or if you fancy the challenge use XML parsing and XQuery to pull out the ParameterList tags


A query that give the login name, hour of execution and query as text of last events, if you can give a hand to the join of sys.dm_exec_query_stats, but it works

SELECT distinct
s.login_name,
qs.creation_time 
,SUBSTRING(st.text, (qs.statement_start_offset/2) + 1,
((CASE 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_sessions S
LEFT JOIN sys.dm_exec_connections AS c ON S.session_id = c.session_id
,sys.dm_exec_query_stats QS --qs on c.most_recent_sql_handle = qs.sql_handle
OUTER APPLY sys.dm_exec_sql_text(qs.sql_handle) AS st
WHERE ST.text LIKE '%yourKeyWord%' and s.login_name <> 'NT SERVICE\SQLSERVERAGENT'
order by qs.creation_time desc