Re: Pro*C - extra space after variables

From: Thomas Kyte <tkyte_at_us.oracle.com>
Date: Wed, 08 Dec 1999 14:35:31 -0500
Message-ID: <4hct4sc2rjq1mm7c976fuevumfp3banf8q_at_4ax.com>


A copy of this was sent to Kenneth C Stahl <BlueSax_at_Unforgettable.com> (if that email address didn't require changing) On Wed, 08 Dec 1999 09:20:13 -0500, you wrote:

>Clint Eastwood wrote:
>>
>> HiYa
>>
>> You are bang on with the length of the char[] that I'm selecting into.
>>
>> thanks for the advice. I certainly can see why implicict cursors could
>> cause hassles, especially if there was more than one row brought back
>> when you were expexting only one. however when getting something from
>> dual, is it worth declaring a cursor and then opening/fetching/closing?
>>
>> thanks for the suggestion about the indicator variable
>>
>> I have been having difficulty with using varchar (I am still learning)
>> in the host side systems.
>>
>
>It is absolutely worth it to write the extra code when selecting from dual.

[Quoted] [Quoted] why? how is that largish amount of code better then:

int get_date(void)
{
[Quoted] static varchar today[8];

	exec sql whenever sqlerror goto sqlerr;
	exec sql whenever not_found goto sqlerr;
	exec sql select to_char(sysdate,'yyyymmdd') into :today from dual;
    

    fprintf(stdout,"Today: %.*s",today.len,today.arr);

    return 0;
sqlerr:

        return 1;
}

>It may seem like a lot of code, but in the long run it really isn't all
>that bad. Here is how I would do it:
>
>const int open = 1;
>const int close = 0;
>
>varchar today[8];
>
>int open_cur(int op)
>{
> exec sql declare cursor c_sysdate as
> select to_char(sysdate,'YYYYMMDD') from dual;
>
> exec sql whenever sqlerror goto sqlerr;
>
> if (op == open)
> exec sql open c_sysdate;
> else
> exec sql close c_sysdate;
> return(0);
>slqerr:
> return(1);
>}
>
>int fetch_cur(void)
>{
> exec sql whenever sqlerror goto sqlerr;
> exec sql
> fetch c_sysdate
> into :today;
> return(0);
>sqlerr:
> return(1);
>}
>
>int get_date(void)
>{
> if (open_cur(open) != 0)
> return(1);
>
> memset(&today,'\0',sizeof(today));
> if (fetch_cur() != 0)
> return(1);
>
> if (close_cur() != 0)
> return(1);
>
> fprintf(stdout,"Today: %.*s",today.len,today.arr);
> return(0);
>}
>
>
>
>There are those who will immediately cry out that I did not allow room for
>a null terminator in
>the host variable and that I did not insert a null at the end of the
>returned string. Neither is
>required - which is why I used the today.len value to limit the number of
>characters printed by fprintf(). Also, you will probably want to include
>more code for the error checking as well as
>including an EXEC SQL WHENEVER SQLWARNING ..... clause for the fetch just
>for completeness.

-- 
See http://osi.oracle.com/~tkyte/ for my columns 'Digging-in to Oracle8i'...
Current article is "Part I of V, Autonomous Transactions" updated June 21'st
 
Thomas Kyte                   tkyte_at_us.oracle.com
Oracle Service Industries     Reston, VA   USA

Opinions are mine and do not necessarily reflect those of Oracle Corporation
Received on Wed Dec 08 1999 - 20:35:31 CET

Original text of this message