PRO*C FETCH, is this expected behavior?

From: Frank Peters <fwp_at_Jester.CC.MsState.Edu>
Date: Sun, 1 Mar 1992 06:27:10 GMT
Message-ID: <1992Mar1.062710.13863_at_ra.msstate.edu>


Hello,

I'm in the process of learning PRO*C in ORACLE V6 running on a Sun4 running SunOS 4.1.1. I'm having a problem which seems to indicate that ORACLE isn't null terminating strings after the first FETCH. The result is that FETCHes which yield a shorter string than the previous FETCH have the 'extra' characters from that previous string in the new string.

To illustrate, I have the following table:



SQL> desc susers
 Name                            Null?    Type
 ------------------------------- -------- ----
 USERCODE                        NOT NULL NUMBER
 FNAME                                    CHAR(15)
 MNAME                                    CHAR(15)
 LNAME                                    CHAR(25)
 CNAME                                    CHAR(15)
 OFFICE                                   CHAR(20)
 EADDR                                    CHAR(40)
 DEPTNO                                   NUMBER

SQL> select fname, lname from susers;
FNAME           LNAME                                                           
~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~                                       
Frank           Peters                                                          
Thomas          Ritter                                                          
Tim             Griffin                                                         
Tim             Beyea                                                           
Charles         Dunn                                                            

5 rows selected.


And this is the relevant section of my program. Its purpose is to take a name and print any rows that match that name in the fname or lname columns.



... Declarations and cp argv[1] into charmatch ...

   EXEC SQL DECLARE who_cursor CURSOR FOR

        SELECT FNAME, LNAME, CNAME
	   FROM SUSERS
	   WHERE UPPER(FNAME) = UPPER(:charmatch)
              OR UPPER(LNAME) = UPPER(:charmatch)
              OR UPPER(CNAME) = UPPER(:charmatch);
   

   EXEC SQL OPEN who_cursor;
   EXEC SQL WHENEVER NOT FOUND GOTO no_more;

   for (;;) {

      EXEC SQL FETCH who_cursor INTO :fname, :lname, :cname;
      printf("Full Name: %s %s\n", fname.arr, lname.arr);
   }
... Program termination code ...

Running this program gives the following:



Full Name: Tim Griffin
Full Name: Tim Beyeain

Notice that the second fetch ('Beyea' still has the 'in' from the end of 'Griffin').

I can, of course, get around this by inserting the nulls myself or by explicitly printing variable.len characters. Before I get into the habit of doing one of these in all of my programs, though, I'd like to know if this is really the expected behavior. It seems like null terminating a returned output string would be such a simple thing. :-)

--
Frank Peters   Internet:  fwp_at_CC.MsState.Edu          Bitnet:  Fwp_at_MsState
               Phone:     (601)325-7030               FAX:     (601)325-8921
Received on Sun Mar 01 1992 - 07:27:10 CET

Original text of this message