Call pl/sql function and discard result

oracleplsql

I'm calling a function from pl/sql, but I do not require the result in this case. Is there a cleaner way to call this function without using a dummy variable?

Works:

junk := performWork();

Does not work:

performWork();         --PLS-00221: performWork is not a procedure or is undefined
EXEC performWork();    --this might work from SQL*Plus, but not from pl/sql
SELECT pkg.performWork() FROM DUAL;  --PLS-00428: INTO clause is expected in this SELECT statement

Best Answer

Your options are:

  1. Assign the result to a dummy variable:

    junk := perform_work;
    
  2. Select the result into a dummy variable:

    select perform_work into junk from dual;
    
  3. Create an implicit cursor in a loop:

    for i in ( select perform_work from dual ) loop
       null;
    end loop;
    
  4. If the function does not need to be called from SQL you could change it into a procedure with an OUT parameter, though this still requires a "junk" variable it might look very slightly cleaner?

    perform_work(junk);
    

If you don't want to use this function though, why not just remove it from your code?

The only time I can think of there being a valid use-case for this sort of structure is when you're running identical code in two different schemas/on two different databases but this function is useless in one of them. If that's the case the function is still useful in the other and so you will want to do something with the result anyway.