Skip to main content
1 of 2

How to synchronize multiple instances of a stored procedure executing at the same time in SQL Server 2012?

Overview: In my scenario a user uploads an excel file that needs to be written into database, I am allocating each uploaded file a batch_id that batch_id is written in last column of each row of my table as shown in these pictures File of User 1

enter image description here

With the help of this batch_id column I will be able to segregate that which rows belong to which document.(for example: if batch_id=1 than these 1000 rows belongs to document 1)

Problem: Consider two users user1 and user2 executing this stored procedure at the same time while Stored Procedure is writing batch_id=1 in all the rows which belong to document1,at the same time rows belonging to document2 will gets inserted thus causing a conflict wether to write batch_id=1 or batch_id=2 in the rest of remaining rows.

Imperfect Solution: To Execute Stored Procedure on one document at a time,when its done execute it on document 2 but this will let the user to wait until the current execution finishes which i don't want to happen.