Search code examples
sql-server-2008cursordeadlock

How to eliminate this cursor (causing a deadlock)


We have got a stored procedure. Part of it is executing a cursor over a select statement that involves 6 different tables with inner joins.

In the cursor body we execute 3 other stored procedures with the parameters from the cursor.

Is there any way to get rid of a cursor in this situation?

Thanks all!

  DECLARE myCursor CURSOR FOR
              SELECT x,y,z
              FROM   a
                     INNER JOIN .....
                     INNER JOIN .....
                     INNER JOIN .....
                     INNER JOIN .....
                     INNER JOIN .....

            OPEN myCursor

            FETCH NEXT ...

            WHILE @@FETCH_STATUS = 0
              BEGIN

                    EXECUTE xy @cursor_variable

                    EXECUTE yz @cursor_variable

                    EXECUTE abc @cursor_variable

                  FETCH NEXT FROM myCursor INTO ...
              END

            CLOSE myCursor

            DEALLOCATE myCursor
        END

Solution

  • Try a static cursor, that won't hold on to locks.