Question

I've been doing some research trying to resolve this issue which seems simple but it is not. I am currently connecting to an oracle DB from a C program and executing the following query:

char buf[20];
EXEC SQL SELECT TO_CHAR( modified_time, 'YYYY-MM-DD HH24:MI:SS')
                  INTO :buf :i_modified_time
                  FROM RECORD_ENTRY_TABLE
                 WHERE  record_id_num = rec_id_num;
printf("THe modified time is: %s", buf);

When I check the table using SQL the time in the table is 2008-10-14 13:53:02, but when compile and run the program doesnt print anything at all. I have other queries that work just fine but not this one for some reason. Can you help or make suggestions? Thank you before hand!

Était-ce utile?

La solution

character array but is not initialised,(memset) , that might be an issue! Also check for the Indicator variable status as a good practice.

The ROWNUM in the query I added is not needed if record_id_num is PrimaryKey. but still anyone who read the code, might get a doubt, what if the query returns more than one row. In that case SQLCODE -2112 should be handled.

char buf[19+1];

short i_modified_time = -1;

memset(buf,'\0',sizeof(buf);

EXEC SQL SELECT TO_CHAR( modified_time, 'YYYY-MM-DD HH24:MI:SS')
                  INTO :buf :i_modified_time
                  FROM RECORD_ENTRY_TABLE
                 WHERE  record_id_num = rec_id_num
                 AND ROWNUM < 2;

if(i_modified_time == 0) {
    printf("THe modified time is: <%s>", buf);
}else if(sqlca.sqlcode != 0){
    printf("Error while Query <%s>",sqlca.sqlerrm);
}else {
    printf("THe modified time is not available);
}

Autres conseils

I assume you are using Pro*C. Your query assumes that there is always exactly one record in the table. Make sure that this precondition is met and always handled correctly.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top