check for zero rows in select query

只愿长相守 提交于 2019-12-25 17:45:04

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!