Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
![]() |
![]() |
Home -> Community -> Usenet -> c.d.o.server -> Re: sqlplus acting differently than embedded SQL in C
A copy of this was sent to reilly_at_news.dca.net (G B Reilly)
(if that email address didn't require changing)
On 3 Nov 1998 17:11:41 +0500, you wrote:
>I have a problem performing a FETCH into host arrays
>when the key in the WHERE clause is a foreign key.
>When I try to use the FETCH in a C program I get a 1403
>When I try the identical command in sqlplus I get
>the correct rows returned. The table I am trying
>to FETCH from has a foreign key where the rows
>can have identical keys but different values in
>other columns. I cannot find anything in the sample
>programs or Oracle manuals about this.
>
>Any hints?
When array fetching, the 1403 will be raised by the fetch that hit the 'end of data' and that fetch will have returned some rows. When array fetching using proc, need to use a loop that resembles something like this:
static void process()
{
EXEC SQL BEGIN DECLARE SECTION;
int empno[5];
EXEC SQL END DECLARE SECTION;
int i; int last_row_cnt; int sqlCode;
EXEC SQL WHENEVER SQLERROR DO sqlerror_hard(); EXEC SQL DECLARE C CURSOR FOR SELECT EMPNO FROM EMP; EXEC SQL OPEN C; for( last_row_cnt = 0;
sqlca.sqlcode == 0; last_row_cnt = sqlca.sqlerrd[2] ) { EXEC SQL WHENEVER NOTFOUND CONTINUE; EXEC SQL FETCH C INTO :empno; printf( "===========\nSQLCODE = %d\n", sqlca.sqlcode ); printf( "Last Row Cnt = %d, sqlca.sqlerrd[2] = %d\n", last_row_cnt, sqlca.sqlerrd[2] ); printf( "that means we got %d rows this time around\n\n", sqlca.sqlerrd[2]-last_row_cnt ); for( i = 0; i < sqlca.sqlerrd[2]-last_row_cnt; i++ ) { printf( "empno[%d] = %d\n", last_row_cnt+i, empno[i] ); }
You need to remember the number of rows you've already processed (last_row_cnt in this example). You need to ignore the NOTFOUND warning inside the loop and only break out of the loop after youve processed any and all rows that were fetched. You need to process "sqlca.sqlerrd[2] - last_row_cnt" rows after every fetch -- including the one that raises the 1403 error.
I happen to have 14 rows in my emp table. When I run the above routine, I get as output:
empno[0] = 7369 empno[1] = 7499 empno[2] = 7521 empno[3] = 7566 empno[4] = 7654
empno[5] = 7698 empno[6] = 7782 empno[7] = 7788 empno[8] = 7839 empno[9] = 7844
empno[10] = 7876 empno[11] = 7900 empno[12] = 7902 empno[13] = 7934
Hope this helps...
Thomas Kyte
tkyte_at_us.oracle.com
Oracle Government
Herndon VA
--
http://govt.us.oracle.com/ -- downloadable utilities
Anti-Anti Spam Msg: if you want an answer emailed to you, you have to make it easy to get email to you. Any bounced email will be treated the same way i treat SPAM-- I delete it. Received on Wed Nov 04 1998 - 08:10:54 CST
![]() |
![]() |