Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open and Close Cursors Inside or Outside a Transaction and How to Close a Cursor if a Transaction Fails

I am writing a stored procedure in SQL Server 2012 that uses a cursor for reading and a transaction inside a TRY CATCH block. Basically, my questions are as follows:

  1. Should I declare my cursor inside the TRY CATCH block? If yes, should I declare the cursor before or after the BEGIN TRANSACTION statement?
  2. Should I open the cursor before or after the BEGIN TRANSACTION statement?
  3. Should I close and deallocate the cursor before or after the COMMIT TRANSACTION statement?
  4. Should I close and deallocate the cursor before or after the ROLLBACK TRANSACTION statement if something fails?

Sample T-SQL Code:

DECLARE @ColumnID AS INT;
DECLARE @ColumnName AS VARCHAR(20);
DECLARE @ColumnValue AS FLOAT;

-- Should I declare my cursor inside the TRY CATCH block?
-- If yes, should I declare the cursor before or after the BEGIN TRANSACTION statement?

DECLARE myCursor CURSOR LOCAL FAST_FORWARD FOR
    SELECT
        a.ColumnID,
        a.ColumnName,
        a.ColumnValue

    FROM
        MyTable a;

BEGIN TRY

    -- Should I open the cursor before or after the BEGIN TRANSACTION statement?

    BEGIN TRANSACTION myTransaction;

    OPEN myCursor;

    FETCH NEXT FROM myCursor INTO @ColumnID, @ColumnName, @ColumnValue;

    WHILE @@FETCH_STATUS = 0 BEGIN

        IF (@ColumnName IS NULL) BEGIN

            UPDATE
                MyTable

            SET
                @ColumnValue = NULL

            WHERE
                ColumnID = @ColumnID;

        END;

        FETCH NEXT FROM myCursor INTO @ColumnID, @ColumnName, @ColumnValue;

    END;

    -- Should I close and deallocate the cursor before or after the COMMIT TRANSACTION statement?

    CLOSE myCursor;
    DEALLOCATE myCursor;

    COMMIT TRANSACTION myTransaction;

END TRY
BEGIN CATCH

    -- Should I close and deallocate the cursor before or after the ROLLBACK TRANSACTION statement:

    IF CURSOR_STATUS('local', 'myCursor') = 1 BEGIN

        CLOSE myCursor;
        DEALLOCATE myCursor;

    END;

    ROLLBACK TRANSACTION myTransaction;

END CATCH;
like image 405
HydroPowerDeveloper Avatar asked Jan 20 '14 02:01

HydroPowerDeveloper


People also ask

How do I close an open cursor in SQL?

EXEC SQL CLOSE cursor-name END-EXEC. If you processed the rows of a result table and you do not want to use the cursor again, you can let the system close the cursor. The system automatically closes the cursor when: A COMMIT without HOLD statement is issued and the cursor is not declared using the WITH HOLD clause.

How do I close my cursor?

You can also close a cursor implicitly using a COMMIT statement or, for cursors defined with WITH HOLD, using a ROLLBACK statement.

What happens if you don't close a cursor?

Not closing a cursor will keep locks active that it holds on the rows where it is positioned.

What does closing a cursor do in SQL?

Closes an open cursor by releasing the current result set and freeing any cursor locks held on the rows on which the cursor is positioned. CLOSE leaves the data structures available for reopening, but fetches and positioned updates are not allowed until the cursor is reopened.


Video Answer


1 Answers

I would declare and open the cursor before the BEGIN TRY and then close and deallocate it after the END CATCH to minimize the amount of time you're spending in the transaction. This also means you don't need to write the close/deallocate statements twice.

My second choice would be to declare and open the cursor inside the BEGIN TRANSACTION and then close and deallocate before the ROLLBACK. I'm sure others will prefer this style.

These ways the cursor is either entirely outside the try/catch and transaction or entirely contained within them. Doing it otherwise feels like crossing scopes to me, but would certainly still work. I think this question is mainly an issue of style

like image 161
Matthew Jaspers Avatar answered Sep 22 '22 01:09

Matthew Jaspers