0% found this document useful (0 votes)
207 views13 pages

How To Check Stored Procedure Execution History in SQL Server - SADA Tech

Uploaded by

Andry Nary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
207 views13 pages

How To Check Stored Procedure Execution History in SQL Server - SADA Tech

Uploaded by

Andry Nary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

25/04/2024 09:36 how to check stored procedure execution history in SQL server - SADA Tech

how to check stored procedure execution history in SQL server


Admin • 10 February 2024 • Last Update : 1 month ago

Table of Contents

1. How to Check Stored Procedure Execution History in SQL Server


1.1. 1. Using System Views
1.2. 2. Using SQL Server Profiler
1.3. 3. Using Extended Events
1.4. Best Practices for Monitoring Stored Procedure Execution History
1.5. FAQ Section
1.5.1. Q1: Can I check the execution history of a specific stored procedure for a specific user?
1.5.2. Q2: Are there any third-party tools available to monitor stored procedure execution history?
1.5.3. Q3: How can I identify the most frequently executed stored procedures in my database?
1.6. Conclusion

How to Check Stored Procedure Execution History in SQL


Server

https://tech.sadaalomma.com/sql/how-to-check-stored-procedure-execution-history-in-sql-server/ 1/13
25/04/2024 09:36 how to check stored procedure execution history in SQL server - SADA Tech

Stored procedures are an essential part of any SQL Server database. They allow you to encapsulate a set of
SQL statements into a single unit, making it easier to manage and execute complex database operations.
However, keeping track of the execution history of stored procedures can be challenging, especially in a
production environment where multiple users and applications interact with the database.

In this article, we will explore various methods to check the execution history of stored procedures in SQL
Server. We will discuss built-in system views, third-party tools, and best practices to effectively monitor and
analyze the performance of your stored procedures.

https://tech.sadaalomma.com/sql/how-to-check-stored-procedure-execution-history-in-sql-server/ 2/13
25/04/2024 09:36 how to check stored procedure execution history in SQL server - SADA Tech

1. Using System Views


SQL Server provides several system views that store information about the execution history of stored
procedures. These views can be queried to retrieve valuable insights into the performance and usage patterns
of your stored procedures.

One of the most commonly used system views is sys.dm_exec_query_stats, which returns aggregate
performance statistics for cached query plans. By joining this view with other system views, such as
sys.dm_exec_procedure_stats and sys.dm_exec_sql_text, you can obtain detailed information about the
execution history of stored procedures.

SELECT
qs.creation_time,
qs.execution_count,
qt.text
FROM
sys.dm_exec_query_stats AS qs
CROSS APPLY
sys.dm_exec_sql_text(qs.sql_handle) AS qt
WHERE
qt.text LIKE '%YourStoredProcedureName%'
ORDER BY
qs.creation_time DESC;

This query retrieves the creation time, execution count, and text of the stored procedure. You can replace
‘YourStoredProcedureName’ with the name of the stored procedure you want to track. The result will provide
you with a list of execution instances, along with their timestamps and the actual SQL code executed.

https://tech.sadaalomma.com/sql/how-to-check-stored-procedure-execution-history-in-sql-server/ 3/13
25/04/2024 09:36 how to check stored procedure execution history in SQL server - SADA Tech

PL/SQL Stored Procedure Concurrent Program i…


i…
Share


Watch on

Another useful system view is sys.dm_exec_requests, which returns information about the currently executing
requests in SQL Server. By filtering the results based on the command column, you can identify the execution
of stored procedures.

SELECT
r.start_time,
r.command,
t.text
FROM
sys.dm_exec_requests AS r
CROSS APPLY
sys.dm_exec_sql_text(r.sql_handle) AS t
WHERE

https://tech.sadaalomma.com/sql/how-to-check-stored-procedure-execution-history-in-sql-server/ 4/13
25/04/2024 09:36 how to check stored procedure execution history in SQL server - SADA Tech
r.command = 'EXECUTE'
AND t.text LIKE '%YourStoredProcedureName%';

This query retrieves the start time, command type, and text of the executing request. Replace
‘YourStoredProcedureName’ with the name of the stored procedure you want to monitor. The result will
provide you with real-time information about the currently executing instances of the stored procedure.

2. Using SQL Server Profiler


SQL Server Profiler is a powerful tool that allows you to capture and analyze events in SQL Server. It provides
a graphical interface to monitor and trace the execution of stored procedures, along with other database
activities.

To track the execution history of stored procedures using SQL Server Profiler, follow these steps:

1. Launch SQL Server Profiler from the SQL Server Management Studio.

2. Create a new trace by clicking on “File” > “New Trace”.

https://tech.sadaalomma.com/sql/how-to-check-stored-procedure-execution-history-in-sql-server/ 5/13
25/04/2024 09:36 how to check stored procedure execution history in SQL server - SADA Tech

3. In the “Events Selection” tab, select the events you want to capture. For stored procedure execution history,
choose the “Stored Procedures” category and select the “RPC:Completed” and “SP:StmtCompleted” events.

4. Specify any additional filters or options based on your requirements.

5. Start the trace and let it run for a specific period.

6. Once the trace is complete, you can analyze the captured events using the various tools provided by SQL
Server Profiler.

https://tech.sadaalomma.com/sql/how-to-check-stored-procedure-execution-history-in-sql-server/ 6/13
25/04/2024 09:36 how to check stored procedure execution history in SQL server - SADA Tech

SQL Server Profiler offers a comprehensive set of features to analyze the captured events, such as filtering,
grouping, and sorting. You can also save the trace results for future reference or share them with other team
members.

3. Using Extended Events


Extended Events is a lightweight and highly customizable event-handling system in SQL Server. It provides a
flexible framework to capture and analyze events at a low overhead, making it suitable for monitoring the
execution history of stored procedures.

To track the execution history of stored procedures using Extended Events, follow these steps:

1. Launch SQL Server Management Studio and connect to the target SQL Server instance.

2. Expand the “Management” node, right-click on “Extended Events”, and select “New Session”.

https://tech.sadaalomma.com/sql/how-to-check-stored-procedure-execution-history-in-sql-server/ 7/13
25/04/2024 09:36 how to check stored procedure execution history in SQL server - SADA Tech

3. In the “New Session” dialog, specify a name for the session and choose the desired event template. For
stored procedure execution history, you can select the “sql_statement_completed” event.

4. Configure any additional options or filters based on your requirements.

5. Start the session and let it run for a specific period.

6. Once the session is complete, you can analyze the captured events using the various tools provided by SQL
Server Management Studio.

https://tech.sadaalomma.com/sql/how-to-check-stored-procedure-execution-history-in-sql-server/ 8/13
25/04/2024 09:36 how to check stored procedure execution history in SQL server - SADA Tech

Extended Events offer a wide range of options to customize the event capture, such as filtering, grouping, and
aggregating. You can also save the session results for further analysis or share them with other team
members.

Best Practices for Monitoring Stored Procedure Execution History


While the methods mentioned above provide valuable insights into the execution history of stored
procedures, it is essential to follow best practices to ensure effective monitoring and analysis. Here are some
tips to consider:

Regularly review the execution history: Set up a schedule to review the execution history of critical stored
procedures. This will help you identify any performance issues or bottlenecks and take appropriate actions.

Monitor execution times: Keep track of the execution times of your stored procedures. If you notice any
significant variations or spikes, investigate the underlying causes and optimize the code if necessary.

Use performance counters: Monitor relevant performance counters, such as CPU usage, memory usage,
and disk I/O, to identify any resource-related issues that may impact the execution of stored procedures.

https://tech.sadaalomma.com/sql/how-to-check-stored-procedure-execution-history-in-sql-server/ 9/13
25/04/2024 09:36 how to check stored procedure execution history in SQL server - SADA Tech

Optimize query plans: Analyze the query plans of your stored procedures to ensure they are efficient and
utilize the available indexes and statistics. Use tools like SQL Server Query Store to identify and fix any plan
regressions.

Implement logging and error handling: Include logging and error handling mechanisms in your stored
procedures to capture any runtime errors or exceptions. This will help you troubleshoot issues and track the
execution flow.

FAQ Section

Q1: Can I check the execution history of a specific stored procedure for a
specific user?

A1: Yes, you can filter the results of the system views or trace events based on the user name. For example,
you can modify the queries mentioned earlier to include a condition like “AND r.login_name =
SADA TECH to retrieveSearch
‘YourUserName'”
... Technology About Sada Tech
the execution history of a specific stored procedure for a specific user.

Q2: Are there any third-party tools available to monitor stored procedure
execution history?

A2: Yes, several third-party tools offer advanced monitoring and analysis capabilities for stored procedure
execution history. Some popular options include SQL Sentry, Redgate SQL Monitor, and SolarWinds Database
Performance Analyzer. These tools provide comprehensive dashboards, alerts, and reports to help you track
and optimize the performance of your stored procedures.

https://tech.sadaalomma.com/sql/how-to-check-stored-procedure-execution-history-in-sql-server/ 10/13
25/04/2024 09:36 how to check stored procedure execution history in SQL server - SADA Tech

Q3: How can I identify the most frequently executed stored procedures in
my database?

A3: You can use the system view sys.dm_exec_procedure_stats to retrieve information about the execution
count of stored procedures. By sorting the results based on the execution count in descending order, you can
identify the most frequently executed stored procedures in your database.

Conclusion
Monitoring the execution history of stored procedures is crucial for maintaining the performance and stability
of your SQL Server database. By leveraging the built-in system views, SQL Server Profiler, and Extended
Events, you can gain valuable insights into the usage patterns and performance characteristics of your stored
procedures. Additionally, following best practices and using third-party tools can further enhance your
monitoring and analysis capabilities, allowing you to optimize the execution of your stored procedures and
improve overall database performance.

References:

[1] sys.dm_exec_query_stats

[2] sys.dm_exec_procedure_stats

[3] sys.dm_exec_sql_text

[4] sys.dm_exec_requests

[5] SQL Server Profiler

https://tech.sadaalomma.com/sql/how-to-check-stored-procedure-execution-history-in-sql-server/ 11/13
25/04/2024 09:36 how to check stored procedure execution history in SQL server - SADA Tech

[6] Extended Events

Related News

SQL server agent starts and stops Sql Server Lock a Table server collation in SQL server
immediately

Leave a Comment

Your email address will not be published. Required fields are marked *

Comment*

Name*

Email*

Website

https://tech.sadaalomma.com/sql/how-to-check-stored-procedure-execution-history-in-sql-server/ 12/13
25/04/2024 09:36 how to check stored procedure execution history in SQL server - SADA Tech

Comments Rules :

Submit

Technology

Excel Sheets Docs About Sada Tech

Sada Tech © 2024 All rights reserved.

SADA TECH

https://tech.sadaalomma.com/sql/how-to-check-stored-procedure-execution-history-in-sql-server/ 13/13

You might also like