Skip to content
Advertisement

How to persist SQL Server system table records?

I am running a query that returns the last execution time for a stored procedure:

I am getting the correct results, but if I run the query again in around 30 seconds, I don’t get any results.

Is there a setting or command I need to set or add to persist the results?

My goal is to find out what stored procedures ran in the past 3 days. I’m running this query against SQL Server 2019 Express.

Advertisement

Answer

I would suggest extended events for this. First, the session definition:

You may want to modify the session definition to suit your needs. Examples would be:

  • Filtering by a particular user (e.g. your application’s login)
  • Grabbing just a sample (e.g. “one in a hundred executions”)
  • Grab additional data (e.g. “what user called the proc?”, “what was the whole statement (including parameters)?”, etc)

Here’s how to read the data:

Again, this is very basic (returning only the timestamp, database, and procedure name). When I set about munging a new event session’s data, I’ll use that event column as a reference for what the XML looks like so I can write appropriate xpath expressions to pull the data that I need.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement