FIX: Access violation when you execute a stored procedure that uses a cursor on a table variable in SQL Server

Symptoms

Assume that you create a stored procedure that uses a cursor on a table variable in Microsoft SQL Server 2012 or SQL Server 2014. Additionally, the stored procedure updates the table by using a WHERE CURRENT OF statement together with the cursor.

DECLARE ccc CURSOR LOCAL FOR SELECT SomeInt FROM @TableVar WHERE SomeInt IS NULL

OPEN ccc

FETCH NEXT FROM ccc INTO @curInt UPDATE @TableVar SET SomeInt = @newInt WHERE CURRENT OF cccCLOSE ccc

SELECT * FROM @TableVar END

When you execute your stored procedure by using the sp_refreshsqlmodule system stored procedure, an access violation may occur, and you may receive error messages that resemble the following:

Msg 596, Level 21, State 1Cannot continue the execution because the session is in the kill state.Msg 0, Level 20, State 0A severe error occurred on the current command. The results, if any, should be discarded.

In this situation, errors that resemble the following are also written to the SQL Server error logs:

Each new cumulative update for SQL Server contains all the hotfixes and all the security fixes that were included with the previous cumulative update. Check out the latest cumulative updates for SQL Server: