SQL Server 2008 R2 – Can Extended Events Execute Stored Procedure or Query?

extended-eventsperformancesql serversql-server-2008-r2

I'm trying to use extended events to track queries that never complete using Event Pairing as a target. (see this question I asked a few days ago). The event pairing target should let me find queries that started but never completed.

My new problem is that I need to know what else was happening on the server when this event happens. I'd like to execute a stored procedure like sp_who, sp_who2, sp_WhoIsActive, etc. whenever I find a query that has not completed in 20+ seconds, for example. Is this possible?

Best Answer

Out of the box, no.

You'd need to hook into the event stream for extended events and then take action based on that. Tom Stringer has a good overview and sample code to do this!

See also Introducing the Extended Events Reader by Mike Wachal.

Jonathan Kehayias also describes it in detail in his Pluralsight course.