Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Getting the SQL from a EXEC sp_prepare statement captured in profiler

$
0
0

I am watching server activity and see the occasional EXEC sp_execute ?;.  I would like to connect the number on the execute to a text representation of the SQL.

If you execute the following in SSMS, you can see the output and the value of @i created by the sp_prepare.

DECLARE @i INT;

EXEC sp_prepare @i OUT, NULL, N'SELECT 1',1 ;

PRINT @i;

EXEC sp_execute @i;

--EXEC sp_unprepare @i;

Then in another window execute this:
SELECT * 
FROM sys.dm_exec_cached_plans cp
CROSS APPLY sys.dm_exec_sql_text(cp.plan_handle)
WHERE objtype = 'Prepared';
You can see that the prepared statement is a cached plan and the SQL that it represents.  What i can't figure out is how to connect the number from the trace (if that is where you saw it) to the cached plan.
Does anyone know how to do this?
thanks
danny


-- dan

Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>