I have a stored procedure that has an output parameter of the CURSOR VARYING
type. I would like to verify that the output cursor can be used by the code that called the stored procedure. It seemed that CURSOR_STATUS
was the right function to use, but I'm getting unexpected results when applying it to my output cursor. The function returns a value of -3 inside the stored procedure that created it but works as expected outside the stored procedure. See the code below:
CREATE OR ALTER PROCEDURE dbo.OutputCursorTest
(@Cursor_OUT CURSOR VARYING OUTPUT)
AS
BEGIN
SET NOCOUNT ON;
SET @Cursor_OUT = CURSOR FORWARD_ONLY STATIC FOR
SELECT [high]
from master..spt_values
OPEN @Cursor_OUT;
SELECT CURSOR_STATUS('variable', '@Cursor_OUT'); -- this seems to always return -3
-- possible workaround
/*
DECLARE @Cur_Copy CURSOR;
SET @Cur_Copy = @Cursor_OUT;
SELECT CURSOR_STATUS('variable', '@Cur_Copy');
DEALLOCATE @Cur_Copy;
*/
RETURN;
END;
GO
DECLARE @Cur CURSOR;
EXEC dbo.OutputCursorTest @Cursor_OUT = @Cur OUTPUT;
SELECT CURSOR_STATUS('variable', '@Cur'); -- this returns 1 as expected
I am on SQL Server 2019 CU14 if it matters. Why does CURSOR_STATUS
return a value of -3 ("A cursor with the specified name does not exist.") inside of the stored procedure?