SQL Server – Find Who Executed a Stored Procedure

sql serverstored-procedures

Is there a way/any functions/DMV's in SQL Server that will provide who executed a stored procedure of a particular database?

Best Answer

If the stored procedure already ran and you're looking for the smoking gun, and you're looking for history without setting something up ahead of time, you'll need to analyze the transaction log.

If you're asking because you're ready to change the server or the database to be prepared the next time it happens, you can:

  • Add a line to the stored procedure to log that information to a table
  • Run a trace with Profiler or Extended Events, focusing just on that stored procedure
  • Implement auditing
  • If the stored procedure runs for a long time, you could log sp_WhoIsActive to a table every few minutes via an Agent job, and then later go back through those logs to see who was running it