Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

Need advice with the below stored procedure to avoid concurrency problems..


SQL
Create PROCEDURE [SD_SaveUpdate_DepartmentDetails]                          
@DeptID int,
@DepartmentName varchar(100),
@DepartmentID int out   

AS                            
BEGIN
Begin Try 

-------------------------Insert Update Department Details-----------------------    

Begin  

SET NOCOUNT ON
 
BEGIN TRAN Department
 
IF EXISTS(SELECT * FROM DepartmentTable WITH (UPDLOCK, HOLDLOCK) WHERE DepartmentName= @DepartmentName  and Isdeleted=0)
BEGIN
      UPDATE DepartmentTable SET DepartmentName= @DepartmentName   WHERE DeptID = @DeptID
      set @DepartmentID=@DeptID
END
ELSE
BEGIN
      INSERT INTO DepartmentTable (DepartmentName)  VALUES (@DepartmentName) 
      set @DepartmentID=@@IDentity
END
 
COMMIT Transaction Department
End   
-------------------------Insert Update Department Details-----------------------    
End Try
Begin Catch
set @DepartmentID=0
End Catch
End

Is my above approach using the (UPDLOCK,HOLDLOCK) correct.

I want to set the @DepartmentID output paramater = "-1" if the department is not saved for the USER2 because its a duplicate.Where do i need to add the set @DepartmentID= -1 line in the above SP.

In the catch block i am setting @DepartmentID=0 if there is some error while saving.But i also want to set @DepartmentID=-1 when duplicate occcurs.

Can you please help me with this SP as well as optimitic(cost effective) so that i can use the same kinda logic in all the stored procedures.

Thanks,

Prathap.
Posted
Updated 6-Jul-14 0:13am
v4

1 solution

Thank you all for the time.Without using the Locking concept(SET TRANSACTION ISOLATION LEVEL) or any UPDLOCK, i tried the below SP
SQL
Create PROCEDURE [SD_SaveUpdate_DepartmentDetails]                          
@DeptID int,
@DepartmentName varchar(100),
@DepartmentID int out   


AS                            
BEGIN
Begin Try 
 
-------------------------Insert Update Department Details-----------------------    

Begin  
 
SET NOCOUNT ON
 
BEGIN TRAN Department
 
IF Not EXISTS(SELECT * FROM DepartmentTable  WHERE DepartmentName= @DepartmentName
  and Isdeleted=0 and DeptID <> @DeptID)
and @DeptID > 0 
BEGIN
      UPDATE DepartmentTable SET DepartmentName= @DepartmentName,lastupdated=getdate()   WHERE DeptID = @DeptID
      set @DepartmentID = @DeptID
END
ELSE IF NOT EXISTS(SELECT Top 1 DepartmentName FROM DepartmentTable  WHERE DepartmentName= @DepartmentName and IsDeleted=0)
and @DeptID=0
BEGIN
      INSERT INTO DepartmentTable (DepartmentName,isdeleted,lastupdated)  VALUES (@DepartmentName,0,getdate()) 
       set @DepartmentID=Scope_IDentity()
END
Else
Begin
set @DepartmentID = -1
End
 
COMMIT Transaction Department
End   
-------------------------Insert Update Department Details-----------------------    
End Try
Begin Catch
set @DepartmentID = 0
End Catch
End


To confirm if there is any duplicate value,I tested this SP using SQLQUERYSTRESS tool for multiple users scenario (concurrency) and its successful.No duplicate values.

Please let me know in case of any suggestions.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900