1

Can anybody please tell how to declare a cursor name dynamically (fixed name + unique name) in SQL Server?

This is to prevent the error

02-25-2018 10:12:01 ERROR (AdHocReportserviceImpl.java:882) : org.springframework.jdbc.UncategorizedSQLException: CallableStatementCallback; uncategorized SQLException for SQL [{call usp_AdHocReport(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)}];
SQL state [34000]; error code [16916]; [FMWGEN][SQLServer JDBC Driver][SQLServer]A cursor with the name 'FetchRegion' does not exist.; nested exception is java.sql.SQLException: [FMWGEN][SQLServer JDBC Driver][SQLServer]A cursor with the name 'FetchRegion' does not exist

while accessing it from multi threaded java application. I've tried adding LOCAL after CURSOR declaration but it doesn't work.

DECLARE FetchRegion CURSOR READ_ONLY FOR
    SELECT ......

OPEN FetchRegion 

FETCH NEXT FROM FetchRegion INTO @RGN

WHILE @@ROWCOUNT <> 0
BEGIN
    .....
    FETCH NEXT FROM FetchRegion INTO @RGN
END 

CLOSE FetchRegion
DEALLOCATE FetchRegion
13
  • 1
    Is this cursor used within an stored procedure? Commented Feb 25, 2018 at 14:11
  • Yes it is used in a stored procedure which is being called from a java application. Commented Feb 25, 2018 at 14:41
  • Actually i'm using local DECLARE FetchYear CURSOR READ_ONLY FOR but its still giving doesn't exist error Commented Feb 25, 2018 at 14:46
  • Please find my structure above Commented Feb 25, 2018 at 14:52
  • Could you try with: DECLARE FetchRegion CURSOR LOCAL FAST_FORWARD FORWARD_ONLY FOR Commented Feb 25, 2018 at 14:59

3 Answers 3

1

You can use this sample, i used a system table to get the first 10 rows, but you can create your own cursor.

DECLARE @cursor_name AS NVARCHAR(100)

SET @cursor_name = 'sampleCursor' 
                   + Replace(Cast(Newid() AS VARCHAR(36)), '-', '') 

DECLARE @cursor_sql AS NVARCHAR(max) 

SET @cursor_sql = N'   DECLARE @name nvarchar(10)  DECLARE ' + @cursor_name + N' CURSOR FOR   select top 10 name from sys.all_columns OPEN ' + @cursor_name 
                  + N' FETCH NEXT FROM ' + @cursor_name 
                  + N' INTO @name WHILE @@FETCH_STATUS <> -1 BEGIN print @name FETCH NEXT FROM  ' + @cursor_name + N' INTO @name end CLOSE  ' 
                  + @cursor_name + N' DEALLOCATE  ' + @cursor_name 

PRINT @cursor_sql 

EXECUTE Sp_executesql 
  @cursor_sql 

To the cursor name is added a guid, to guarantee that is always different.

Then a query is created and executed based on that name.

Hope it helps!

Sign up to request clarification or add additional context in comments.

Comments

1

Not sure if anyone else will find this from a similar search, but we had a similar problem with duplicate cursor names and executing dynamic sql is a security nightmare. We resolved the issue by naming the cursor with a scope-temporary prefix (#) and it seemed to resolve the issue.

DECLARE #iter CURSOR FOR ...

OPEN #iter
FETCH NEXT FROM #iter INTO ...

WHILE @@FETCH_STATUS = 0
BEGIN
     ...
     FETCH NEXT FROM #iter INTO ...
END

CLOSE #iter
DEALLOCATE #iter

Comments

0

You need to run it inside a dynamic sql statement. As a starting point you can check this question: Using a cursor with dynamic SQL in a stored procedure

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.