Sql error invalid cursor

This is a reposting of an article Patrick Roth originally wrote on my Developing for Dynamics GP blog. In a recent case of mine, the ISV was running into a common issue that I’ve run into bef…

Patrick Roth - Click for blog homepageThis is a reposting of an article Patrick Roth originally wrote on my Developing for Dynamics GP blog.

In a recent case of mine, the ISV was running into a common issue that I’ve run into before more than a few times.  Actually something I’ve done a few times myself.

The ISV was attempting to use pass through sanScript to populate a SQL temp table.

What he found was that the first SQL_Execute() statement was OK but the subsequent statements would fail with the error:

SQL Errror: 0 [Microsoft][SQL Server Native Client 10.0]Invalid cursor state

The Dexterity code I’d potentially use (omitting error checking and variable declarations).

Code Example

SQL_Connect(SQL_connection);
SQL = "use TWO";
status = SQL_Execute(SQL_connection, SQL);

if status = 0 then
  SQL = "select CUSTNMBR from RM00101";
  status = SQL_Execute(SQL_connection, SQL);
  {get all customer numbers in customer master and add to temp table.}
  if status = 0 then
    status = SQL_FetchNext(SQL_connection);
    while status <> 31 do
      clear table myTempTable;
      SQL_GetData(SQL_connection,1,'Master ID' of table myTempTable);
      save table myTempTable;
      status = SQL_FetchNext(SQL_connection);
    end while;
  end if;
  SQL = "select VENDORID from PM00200";

  status = SQL_Execute(SQL_connection, SQL);
  {get all vendors add to temp table.}
  if status = 0 then
    status = SQL_FetchNext(SQL_connection);
    while status <> 31 do
      clear table myTempTable;
      SQL_GetData(SQL_connection,1,'Master ID' of table myTempTable);
      save table myTempTable;
      status = SQL_FetchNext(SQL_connection);
    end while;
  end if;
end if;
SQL_Terminate(SQL_connection);

What I would find is that my table wouldn’t be populated with the Vendor ID values.

Debugging this (or adding the call to SQL_GetError()) I’d find that the second SQL_Execute() function is what is failing with the error noted.

Why does this happen?

The reason is that I have returned a recordset from the second call.

We note that the first call actually set my database context with the “use TWO” statement.

Because no recordset was generated, the call to the RM00101 table does not fail.

However as the second SQL_Execute() statment reading the RM00101 can return a recordset (even if it doesn’t due to a where clause), the next SQL_Execute() statement will fail with the ‘invalid cursor state’ error.

How do we fix this?  Do we have to terminate the connection?

No – nothing that drastic and the fix is rather easy.

We just need to clear the previous results – Dexterity has the function SQL_Clear() to do this.

So to resolve our issue, we would just need to add this to the above code.

Revised Code Snippet

<customer query omitted for clarity>

{clear previous results to avoid 'invalid cursor state' errors}
SQL_Clear(SQL_connection);
SQL = "select VENDORID from PM00200";
status = SQL_Execute(SQL_connection, SQL);

{get all vendors add to temp table.}
if status = 0 then
  status = SQL_FetchNext(SQL_connection);
  <code to loop through the records omitted for clarity>

Hope this helps out,

Patrick Roth
Senior Escalation Engineer
Dynamics GP Developer Support

PS. Yes I know, the queries are rather lame and would be better performed as one UNION query.  And of course I’m ignoring the possibility of duplicates here.  And yes the BEST way to fill the temp table would be to do an insert query into a temp table.  But those are the types of things you ignore in order to get a simple example.

This article was originally posted on the Developing for Dynamics GP Blog and has been reposted on http://www.winthropdc.com/blog.

SQL Server 2014 Developer SQL Server 2014 Enterprise SQL Server 2014 Standard SQL Server 2012 Service Pack 3 SQL Server 2012 Developer SQL Server 2012 Enterprise SQL Server 2012 Standard SQL Server 2012 Express More…Less

Symptoms

When you use the SQLSetPos function call to run a common table expression (CTE) query to retrieve spatial data, the function call can’t set the cursor position, and you receive an «Invalid cursor position» error message. This problem occurs in Microsoft SQL Server 2014 and Microsoft SQL Server 2012.

Resolution

The issue was first fixed in the following cumulative update of SQL Server:

  • Cumulative Update 13 for SQL Server 2014

  • Cumulative Update 6 for SQL Server 2014 SP1

  • Cumulative Update 1 for SQL Server 2012 SP3


Note You can find information about the latest SQL Server builds from Where to find information about the latest SQL Server builds.

Hotfix information

Download CenterA hotfix update to resolve this problem is available for manual download and installation from the Microsoft Download Center.

Download
Download update 3100451

Microsoft scanned this file for viruses by using the most current virus-detection software that was available on the date that the file was posted. The file is stored on security-enhanced servers that help prevent any unauthorized changes to it.

PrerequisitesThere are no prerequisites for installing this update.

Registry informationTo install this hotfix, you don’t have to make any changes to the registry.

Restart requirementYou may have to restart the computer after you install this hotfix.

Hotfix replacement informationThis hotfix doesn’t replace a previously released hotfix.

Status

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

Need more help?

HI,

Version : Microsoft SQL Server 2014 (SP3-CU4-GDR) (KB4583462) — 12.0.6433.1 (X64)

I have a real basic stored proc that inset data to a table. application is calling it via ODBC. Sometime application receive following error sometime not.

SQL Error:

The statement could not be executed directly — [Microsoft][ODBC SQL Server Driver]Invalid cursor state

I had added SET nocount on in proc but error remain same. dev added open and close conn/cursor before and after that call but it not work. will it work if I add hints like READPAST etc ? or what should I do to get rid of this error

==== SP code

ALTER PROCEDURE [dbo].[spwoInsert]

(

@RELEASED varchar(1),

@USERNAME varchar(28),

@DEPARTMENT varchar(6),

@EMAIL_ADDRESS varchar(105),

@CONTACT varchar(28),

@CUSTOMER_PO varchar(28),

@CUSTOMER_NO varchar(9),

@WEB_REFERENCE_NO int,

@NOTE varchar(1152),

@AE_EMAIL varchar(150),

@WEB_HOLD varchar(1),

@OrderChannel varchar(20),

@SFCaseNo varchar(15),

@SFCaseRecordID varchar(18)

)

AS

BEGIN

declare @ttime datetime;

set @ttime = cast( (‘1900-01-01’+’ ‘+CONVERT(VARCHAR(8),GETDATE(),108)) as datetime)

INSERT INTO WEB_ORDERS (ORDER_DATE

,ORDER_TIME

,RELEASED

,USERNAME

,DEPARTMENT

,EMAIL_ADDRESS

,CONTACT

,CUSTOMER_PO

,CUSTOMER_NO

,WEB_REFERENCE_NO

,NOTE

,AE_EMAIL

,WEB_HOLD

,OrderChannel

,SFCaseNo

,SFCaseRecordID)

VALUES (

cast(getdate() as date),

@ttime,

@RELEASED ,

@USERNAME ,

@DEPARTMENT ,

@EMAIL_ADDRESS ,

@CONTACT ,

@CUSTOMER_PO ,

@CUSTOMER_NO ,

@WEB_REFERENCE_NO ,

@NOTE ,

@AE_EMAIL ,

@WEB_HOLD ,

@OrderChannel ,

@SFCaseNo ,

@SFCaseRecordID

)

END

Phil Parkin

SSC Guru

Points: 246136

Where is the CURSOR?

If you haven’t even tried to resolve your issue, please don’t expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

thbaig

SSCrazy

Points: 2992

I have not defined any cursor. But may  be it is throwing error due to implicit cursors by rdbms , that is the reason for suggestion about SET NOCOunt ON 🙁

  • This reply was modified 1 year, 4 months ago by  thbaig.

Phil Parkin

SSC Guru

Points: 246136

Ok. Is the app calling the proc in a loop?

If you haven’t even tried to resolve your issue, please don’t expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

ratbak

Ten Centuries

Points: 1140

Is there an insert trigger on WEB_ORDERS? Cursor could be lurking there.

thbaig

SSCrazy

Points: 2992

no , app is not calling it in loop, but this could call several times in a sec

thbaig

SSCrazy

Points: 2992

No trigger on this table

ratbak wrote:

Is there an insert trigger on WEB_ORDERS? Cursor could be lurking there.

Jeff Moden

SSC Guru

Points: 1002259

It’s just an INSERT for C.R.U.D.  I suspect that the inset is sometimes being blocked or failing DRI (FK’s) or some other constraint and it’s simply not being reported.  The «cursor» is behind the scenes where the driver creates a cursor behind the scenes and any constraint or other failure may return the failure simply as the cursor not committing and so being in an invalid state.

I haven’t worked on any front end code for almost 2 decades and so that’s pretty much the extent (pun intended) of the help that I can offer.

thbaig

SSCrazy

Points: 2992

This table has no FK constraint , as this is base table and having PK only

Jeff Moden wrote:

It’s just an INSERT for C.R.U.D.  I suspect that the inset is sometimes being blocked or failing DRI (FK’s) or some other constraint and it’s simply not being reported.  The «cursor» is behind the scenes where the driver creates a cursor behind the scenes and any constraint or other failure may return the failure simply as the cursor not committing and so being in an invalid state.

I haven’t worked on any front end code for almost 2 decades and so that’s pretty much the extent (pun intended) of the help that I can offer.

ratbak

Ten Centuries

Points: 1140

thbaig

SSCrazy

Points: 2992

This part of application is developed in Omnis

I need to confirm ODBC version, Yes windows server upgraded some time back

  • This reply was modified 1 year, 4 months ago by  thbaig.

Phil Parkin

SSC Guru

Points: 246136

Can you post the part of the code which sets up the connection and calls the proc? I know nothing about Omnis, but presumably it’s not too alien.

If you haven’t even tried to resolve your issue, please don’t expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

Steve Jones — SSC Editor

SSC Guru

Points: 730358

This is definitely in the driver or front end that the cursor is being set up. Posting that code may help.

thbaig

SSCrazy

Points: 2992

Dev said he used default settings. I got following on login Audit (profiler), not sure if this can help

— network protocol: TCP/IP

set quoted_identifier on

set arithabort off

set numeric_roundabort off

set ansi_warnings on

set ansi_padding on

set ansi_nulls on

set concat_null_yields_null on

set cursor_close_on_commit off

set implicit_transactions off

set language us_english

set dateformat mdy

set datefirst 7

set transaction isolation level read committed

Phil Parkin wrote:

Can you post the part of the code which sets up the connection and calls the proc? I know nothing about Omnis, but presumably it’s not too alien.

Phil Parkin

SSC Guru

Points: 246136

Nothing in the code you posted shows how the proc is called, how the connection is opened or how the connection is closed. It may be helpful to see that.

If you haven’t even tried to resolve your issue, please don’t expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.


0 Flares



Twitter


0








Facebook


0








Google+


0








LinkedIn


0








Buffer


0








Email









Filament.io






0 Flares


×

ORA-01001: invalid cursor error occurs when you tried to reference a cursor that does not yet exist.
A few scenarios given below.

1. FETCH cursor before opening the cursor.
2. CLOSE cursor before opening the cursor.
3. FETCH cursor after closing the cursor.

See the blow example:

When you write generic cursor you can either use FETCH..,OPEN… and CLOSE cursor statements Or you can use the FOR LOOP for iterating through the cursor. When you use FOR LOOP for iteration no need of Explicit use of FETCH..,OPEN… and CLOSE cursor statements.
The cursor will open automatically when entering FOR LOOP and will close the cursor once the loop ends.

Read:Cursor | Oracle PL/SQL Cursors and example.
Read:ORA-01000: maximum open cursors exceeded.
Read:Oracle Cursors | OPEN ,FETCH and CLOSE Cursor statements.

If you use CLOSE statement after the FOR LOOP Oracle will throw the error :
ORA-01001:Invalid Cursor

See the example below

create or replace procedure Param_Cursor as

CURSOR PERSON_CUR(pAge NUMBER) is
Select * from Person p where p.age = pAge;

BEGIN
    for i in PERSON_CUR(13) loop
       dbms_output.put_line(i.firstname);
    end loop;
 close PERSON_CUR;
END Param_Cursor;

Execute this:

SQL&gt; exec Param_Cursor;

begin Param_Cursor; end;

ORA-01001: invalid cursor
ORA-06512: at "TEST.PARAM_CURSOR", line 10
ORA-06512: at line 1

SQL&gt;

Technorati Tags:
ORA-01001, CLOSE cursor, open cursor, Fetch cursor, Oracle cursor


0 Flares



Twitter


0








Facebook


0








Google+


0








LinkedIn


0








Buffer


0








Email









Filament.io






0 Flares


×

Понравилась статья? Поделить с друзьями:
  • Sql error invalid column name
  • Sqlite error or missing database
  • Sqlite error no such column
  • Sqlite error near where syntax error
  • Sqlite error near select syntax error