Skip to content
Advertisement

PL/SQL: IF or CASE statement to check two variables in a single row and return a third variable?

I need PL/SQL code to scan a table (tied to page 5 in Apex), see if a row with two variables (a specific ID and a specific number between 1-3) exists, and return a third variable from that row into an Oracle Apex item (this is a dynamic action on page 10) if it does. I can do this with a SELECT statement and it works properly:

But I can’t use that statement alone because if the row doesn’t exist, it’ll throw an error about missing data. So I need this as an IF or CASE. But when I try to write it as an IF or CASE instead, it returns nothing.

Sample data would be like (all columns are NUMBER types):

It validates fine and throws no errors, but it doesn’t give any result either. If I add an else :P10_ITEM := ‘0’; then it will return 0, so the submit/return is fine and I know the dynamic action is running. Any advice?

Advertisement

Answer

Regarding the 1st SELECT statement you posted and your comments about it:

But I can’t use that statement alone because if the row doesn’t exist, it’ll throw an error about missing data.

There is a simple way out of it – aggregate function.

For example, Scott’s dept table:

This is your current query, and it ends up with no_data_found:

Or, in PL/SQL (which is what Apex uses):

But, if you e.g. MAX it (the column), then the result is NULL, no error any more:

See? Now just check variable’s value and do whatever you want to do.


In your case:

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement