I want to use the function SQL%ROWCOUNT as a way to tell me if a record is in a table or not.
The code that I have is the following:
DECLARE
v_emp employee%ROWTYPE;
CURSOR c_emp IS
SELECT * FROM employee WHERE name='chuck';
BEGIN
OPEN c_emp;
FETCH c_emp INTO v_emp;
IF SQL%ROWCOUNT=1 THEN
DBMS_OUTPUT.PUT_LINE('found');
ELSE
DBMS_OUTPUT.PUT_LINE(TO_CHAR('not found'));
END IF;
END;
But it does not print anything at all, even though that record with that name exists in the database
Normally, you’d do something like
If you really want to use an explicit cursor, you would need to check the
<<cursor_name>>%rowcount, notsql%rowcountto determine how many rows had been fetched. If you’re going to use an explicit cursor, you also need to take care to close the cursor. Since you didn’t post your table definition or the data you’re using, I’ll use theEMPtable in theSCOTTschema as an exampleAnd note that no matter what approach you use, if you want the output from
DBMS_OUTPUTto be displayed, you need to enable output in whatever tool you are using. If you are using SQL*Plus, that would mean runningbefore executing the anonymous PL/SQL block.