Re: Pro*C problem - declare cursor

From: Tommy Wareing <p0070621_at_cs3>
Date: 16 Jun 1994 13:49:30 GMT
Message-ID: <2tpl9a$sug_at_cs3.brookes.ac.uk>


vkwan1_at_vaxa.hofstra.edu wrote:
: Hi all,
: I am very new to Pro*C, and I am having problem in declaring
: a "global" cursor, which other sub-programs can access it.
: The following C program calls 5 small sub programs, and each one
: of those does its own task.
 : I declare a cursor in sub_declare_cursor() as "employee", however,
: when the main program calls sub_fetch_info() to fetch some data from
: the table, an error message prints out.
 

: PCC-S-0014: Undeclared SQL Identifier "EMPLOYEE: at line in file
: sub_fetch_info.pc
 

: The problem is very obvious, when the program leaves
: sub_declare_cursor(), it losses the cursor declaration, that is why
: inside sub_fetch_info(),it doe NOT recognize the cursor, "employee",
: has been declared and opened.

Nope :) The problem is that the pre-compiler is one pass and only knows about declarations it has already seen, irrespective of when they're executed.
In fact, if you look at the C output from the declare statement, you should see that it does nothing: it's just a directive to the precompiler.

: How to declare a cursor in one sub-program, and this cursor can be
: seen in other sub-programs?
 

AFAIK, don't! Even if you include a declare statement in each file, there's no guarantee that, this cursor you fetch from in one file is going to be the cursor declared in another.

In the example you've given, the statements are only one line anyway, so why make them into separate files? In fact, this example is so simple, why even make them functions? In most cases, my code tends to run:
  EXEC SQL DECLARE thingy CURSOR FOR ...;   EXEC SQL OPEN thingy;
  while (TRUE) {
    EXEC SQL FETCH thingy INTO ...;
    if (sqlca.sqlcode==1403) break;
    /* Whatever processing here */
  }
  EXEC SQL CLOSE thingy;

which shows the entire life of the cursor in 8 lines, rather than the 4 different files you use. (I can see a case for having such routines as connecting and locking in a separate place). Received on Thu Jun 16 1994 - 15:49:30 CEST

Original text of this message