Re: Pro*C - extra space after variables

From: Kenneth C Stahl <BlueSax_at_Unforgettable.com>
Date: Wed, 08 Dec 1999 09:20:13 -0500
Message-ID: <384E691D.ADB8D176_at_Unforgettable.com>


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] 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. Received on Wed Dec 08 1999 - 15:20:13 CET

Original text of this message