Solved

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

Posted on 2013-06-04
9
1,487 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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

Suggested Solutions

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

762 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now