I opened a cursor and I want to know if the query returned any records or not. How do I do this? I tried with the "EXCEPTION, WHEN NO_DATA_FOUND" construct, but it doesn't work.
You can use attributes of the cursor to return the number of rows fetched from the cursor. You are probably familiar with the %NOTFOUND attribute to know when to exit from a loop after the last row is returned from the FETCH command. You can also use the %ROWCOUNT attribute to determine how many rows have been fetched from the cursor so far. The following code example shows how %ROWCOUNT can be used:
CURSOR c1 IS SELECT empid,ename FROM emp;
FETCH c1 INTO id,name
EXIT WHEN c1%NOTFOUND;
counter := c1%ROWCOUNT;
DBMS_OUTPUT.PUT_LINE(' Rows processed: '||counter);
Other cursor attributes include %FOUND and %ISOPEN.
Dig Deeper on Oracle database design and architecture