I’m just wondering why this piece of code is not working. I don’t have any supplier id=1 in my table.
DECLARE
VAR SUPP_NM VARCHAR(100);
VAR_SUPP_ID NUMBER := 1;
WHILE_VAR CHAR := 'Y';
BEGIN
SELECT SUPP_NM
INTO VAR_SUPP_NM
FROM TEST.SUPPLIER
WHERE SUPP_ID = VAR_SUPP_ID;
IF SQL%NOTFOUND THEN
DBMS_OUTPUT.PUT_LINE('SQL DATA NOT FOUND');
ELSIF SQL%FOUND THEN
DBMS_OUTPUT.PUT_LINE('DATA FOUND');
END IF;
END;
I get a 01403 error in Toad but not handled as sql%notfound.
Why isn’t the sql%notfound working?
To catch the
NO_DATA_FOUNDexception rewrite your code as follows by addingexceptionsection:Checking
SQL%FOUNDorSQL%NOTFOUNDhave no meaning in the case ofselect intostatement, because if the select statement returns no rows it will always raiseno_data_foundexception, except, if that select statement invokes aggregate function, it will always return data or null if no rows has been selected.Do not use
varchardatatype, usevarchar2datatype instead.