问题
My C program has an embedded sql query. This program runs on windows and queries the oracle database. The query is similar to EXEC SQL SELECT ... I need to add here a check to know if the query returns zero rows.
Basically I want to set a local valiable to know my query has returned no rows and handle this condition accordingly.
How can I add it. I know that EXISTS statement can be used. But I am not getting how do I use it in embedded sql.
Thanks for any help.
回答1:
Use the sqlca struct
EXEC SQL include "sqlca.h"
#define NO_ROWS_FOUND (sqlca.sqlcode==1403)
EXEC SQL BEGIN DECLARE SECTION;
int val=0;
short ind=0;
EXEC SQL END DECLARE SECTION;
EXEC SQL
select value
int :val :ind
from mytable where rownum=1;
if(NO_ROWS_FOUND)
printf("No rows found\n");
回答2:
Use SELECT COUNT(*) FROM ...
and compare result to 0.
来源:https://stackoverflow.com/questions/8039577/check-for-zero-rows-in-select-query