I wrote the below query expecting NO_DATA_FOUND exception to be thrown when no rows found.
BEGIN
SELECT MAX(A_id) + 1 INTO id_variable from table_A;
EXCEPTION
WHEN NO_DATA_FOUND THEN
SELECT MAX(A_id) + 1 INTO id_variable from table_A_archive;
END;
there is no data in table_A but no exception was thrown and eventually the id_variable value is getting null. I google'd and noticed MAX function ignores null values but i could not find any remedy that can make it to throw exception.
How to make it to throw an exception so that control goes for exception and looks into archive table.
is there any other alternative than taking the count() and then getting the value only if count() > 0.
No, it won't go into exception. MAX will not raise no_data_found as it will return a NULL value.
See this:
You could have your custom exception and then raise it when the value is NULL.
For example,
Update If you don't want to raise an exception and just want to select from another table when MAX returns NULL, then add an
IF-ELSE
block.For example,