why single SQL delete statement will cause deadlock?

Posted by George2 on Stack Overflow See other posts from Stack Overflow or by George2
Published on 2010-06-05T16:26:11Z Indexed on 2010/06/05 16:32 UTC
Read the original article Hit count: 202

Filed under:
|

Hello everyone,

I am using SQL Server 2008 Enterprise. I am wondering why even a single delete statement of this store procedure will cause deadlock if executed by multiple threads at the same time?

For the delete statement, Param1 is a column of table FooTable, Param1 is a foreign key of another table (refers to another primary key clustered index column of the other table). There is no index on Param1 itself for table FooTable. FooTable has another column which is used as clustered primary key, but not Param1 column.

create PROCEDURE [dbo].[FooProc]    
(  
 @Param1 int 
 ,@Param2 int  
 ,@Param3 int  
)    
AS    

DELETE FooTable WHERE  Param1 = @Param1     

INSERT INTO FooTable    
 (  
 Param1  
 ,Param2  
 ,Param3  
  )    
 VALUES    
 (  
 @Param1  
 ,@Param2  
 ,@Param3  
  )    

DECLARE @ID bigint    
 SET @ID = ISNULL(@@Identity,-1)    
 IF @ID > 0    
 BEGIN    
      SELECT IdentityStr FROM FooTable WHERE ID = @ID 
 END  

thanks in advance, George

© Stack Overflow or respective owner

Related posts about sql-server-2008

Related posts about deadlock