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.

For example, your stored procedure may resemble the following:
CREATE PROCEDURE dbo.usp_TestSP AS 

BEGIN

DECLARE @TableVar TABLE (SomeInt INT NULL)
INSERT @TableVar VALUES (NULL)

DECLARE @curInt INT, @newInt INT
SET @newInt = 1

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 ccc
CLOSE 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:
date time.730 spid51 SqlDumpExceptionHandler: Process 51 generated fatal exception c0000005 EXCEPTION_ACCESS_VIOLATION. SQL Server is terminating this process.
date time.730 spid51 * *******************************************************************************
date time.730 spid51 *
date time.730 spid51 * BEGIN STACK DUMP:
date time.730 spid51 * date time spid 51
date time.730 spid51 *
date time.730 spid51 *
date time.730 spid51 * Exception Address = 00007FFC270236D5 Module(sqllang+00000000005036D5)
date time.730 spid51 * Exception Code = c0000005 EXCEPTION_ACCESS_VIOLATION
date time.730 spid51 * Access Violation occurred reading address 0000006F00620074
date time.730 spid51 * Input Buffer 136 bytes -
date time.730 spid51 * EXECUTE sp_refreshsqlmodule N'[dbo].[usp_TestSP]'
date time.730 spid51 *
date time.730 spid51 *

Resolution

Cumulative update information

This issue is fixed in the following updates:


Recommendation: Install the latest cumulative update for SQL Server

Status

Microsoft has confirmed that this is a problem in the Microsoft products that are listed in the "Applies to" section.

References

Learn about the terminology that Microsoft uses to describe software updates.
Eigenschaften

Artikelnummer: 3138930 – Letzte Überarbeitung: 31.05.2016 – Revision: 1

Feedback