why deadlock in this SPROC

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sqldebug
    New Member
    • Mar 2009
    • 2

    why deadlock in this SPROC

    I am running a stored procedure on SQL2005.

    This is what the stored procedure contains

    SELECT * FROM Table1 WHERE Id = @IdParam;

    If (@@ROWCOUNT = 0)
    BEGIN
    INSERT INTO Table1 (@IdParam, Value1, Value2, Value3);
    END
    ELSE
    BEGIN
    UPDATE Table1 SET Col1 = Value1, Col2 = Value2, Col3 = Value3 WHERE Id = @IdParam;
    END

    This stored procedure is not called in a transaction.
    When 2 or more processes are trying to execute the stored procedure it causes a dead lock sometimes. I am not able to understand why this would cause a dead lock. Any ideas?
  • ck9663
    Recognized Expert Specialist
    • Jun 2007
    • 2878

    #2
    It's probably because of your UPDATE statement. Make the update more specific so that it will only affect few records and run faster.


    -- CK

    Comment

    • sqldebug
      New Member
      • Mar 2009
      • 2

      #3
      Update is only one record

      I am only updating once record. My Id Column is the primary key.

      Comment

      • ck9663
        Recognized Expert Specialist
        • Jun 2007
        • 2878

        #4
        I can't see anymore reason. Try removing the BEGIN and END. You only have a single statement.

        Also, try to monitor the jobs.

        -- CK

        Comment

        Working...