[Solved] How to write and call an Oracle function in SQL


Generally it is considered bad practice to call a function in SQL which executes SQL. It creates all kinds of problems.

Here is one solution:

create or replace function my_fun
  ( p_sum in number) return varchar2 is
begin
    if p_sum > 100 then return 'ALERT';
    else return 'OK';
    end if;
end;
/

Run it like this:

select id, my_fun(sum(val)) as state
from your_table
group by id;

1

solved How to write and call an Oracle function in SQL