Solved

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

Posted on 2013-06-04
9
1,528 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
  • 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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

831 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