Solved

SQL stored procedure - prevent execution by multiple users at the same time

Posted on 2013-06-04
9
1,562 Views
Last Modified: 2013-06-05
Hi experts,

Is there any way to prevent multiple users from executing a stored procedure at the same time?

Maybe it could just pause execution of their script while the other user completes the  execution?
0
Comment
Question by:JC_Lives
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 11

Assisted Solution

by:Simone B
Simone B earned 150 total points
ID: 39219526
0
 

Author Comment

by:JC_Lives
ID: 39219564
Ok this tells me how to cause the second execution to error-out... but can the second excution just wait until the first is complete? And give a message in the "messages" section saying that it's waiting for another call to execute?
0
 
LVL 11

Expert Comment

by:Simone B
ID: 39219610
No, this will not queue up the requests to run the sp. However, you can customize the message the second user receives, so that they know they have to try again in x amount of time.

I don't recall anything in SQL that will auto-execute the second request. Anyone else out there have an idea?
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 23

Expert Comment

by:Racim BOUDJAKDJI
ID: 39220425
Is there any way to prevent multiple users from executing a stored procedure at the same time?
No direct way.  Stored Procedure are inherently designed for multiuser requests.

Maybe it could just pause execution of their script while the other user completes the  execution?
Just create a table and have a flag value put in it.  At the beginning of the procedure turn the flag to 1 meaning somebody is running the procedure and turn it off to 0 at the end of the procedure.  You then simply have to put a verify condition of the flag: if the flag is 0 run the query else do nothing.
0
 

Author Comment

by:JC_Lives
ID: 39220506
I see what you mean - but if the flag is 1 can we have the second query wait until the flag is back to zero?
0
 
LVL 23

Accepted Solution

by:
Racim BOUDJAKDJI earned 350 total points
ID: 39220532
but if the flag is 1 can we have the second query wait until the flag is back to zero?
Yes you can pause the execution by using the WAITFOR DELAY statement..Something like...

declare @flag as bit
select @flag=column from flagtable

while @flag=1
begin
waitfor delay '00:00:02'
end
update flagtable set column=1
select......(your instructions)
update flagtable set column=0

Open in new window

Hope this helps...
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 39220743
And of course you would need to add TRY ... CATCH to your Stored Procedure to reset the flag to 0 even if there was an error.  Otherwise it is going to be a very long wait...
0
 
LVL 23

Expert Comment

by:Racim BOUDJAKDJI
ID: 39221358
<<Otherwise it is going to be a very long wait>>
Indeed.  Many many things could go wrong and let the flag set to 1 creating an infinite loop.  That is the drawback in changing SQL Server inherent behavior.
0
 

Author Closing Comment

by:JC_Lives
ID: 39222001
Thanks!! Thant really helps!!!
0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question