Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2013-06-04
9
Medium Priority
?
1,663 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 600 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
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
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 1400 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

927 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