Stored procedures are a great way to encapsulate business logic in your database, but we don’t always want the stored procedure to be executed by more than one user at the exact same time. In this article by Hans Michiels, we see how he creates the logic to prevent concurrent execution.
How it works
- You create the table [dbo].[udm_storedproc_executions] (or name it differently) in your database.
- You create the stored procedure [sp].[GetPace] (or name it differently) in your database.
- You add the code snippet below to stored procedures that are not supposed to run multiple times concurrently.
Proof that it works
For the demo I use the following stored procedures:
[sp].[SubStoredProc] is being executed by both other stored procedures.