oracle - Rewrite If-else block using For loop -
how can write following if-else block using loop:
begin if :new.plan_id = 1 select plan_max_start_hh24 new_plan_max_start_hh24 screening_plan plan_id = 1; select plan_min_start_hh24 new_plan_min_start_hh24 screening_plan plan_id = 1; elsif :new.plan_id = 2 select plan_max_start_hh24 new_plan_max_start_hh24 screening_plan plan_id = 2; select plan_min_start_hh24 new_plan_min_start_hh24 screening_plan plan_id = 2; elsif :new.plan_id = 3 select plan_max_start_hh24 new_plan_max_start_hh24 screening_plan plan_id = 3; select plan_min_start_hh24 new_plan_min_start_hh24 screening_plan plan_id = 3; elsif :new.plan_id = 4 select plan_max_start_hh24 new_plan_max_start_hh24 screening_plan plan_id = 4; select plan_min_start_hh24 new_plan_min_start_hh24 screening_plan plan_id = 4; end if; end;
for plan_id
values 1,2,3,4
, have select plan_max_start_hh24
, plan_min_start_hh24
.
why think need loop? i'm assuming plan_id unique number (ie. each of above queries returns @ 1 row).
also, why do 2 selects? can return multiple columns corresponding variables in 1 select statement.
i think you're wanting like:
if :new.plan_id in (1, 2, 3, 4) select plan_max_start_hh24, plan_min_start_hh24 new_plan_max_start_hh24, new_plan_min_start_hh24 screening_plan plan_id = :new.plan_id; end if;
don't forget include exception block take account case above query doesn't return row (and, in case plan_id not unique column, returns multiple rows).
Comments
Post a Comment