can i write a loop inside a function that has if then else if conditions?

前端 未结 2 356
被撕碎了的回忆
被撕碎了的回忆 2021-01-28 01:56

content : i am writing a function to return a value based on 3 conditions, but i need to write a loop inside it so it can check the conditions for each id passed. can i write t

2条回答
  •  孤独总比滥情好
    2021-01-28 02:29

    The way I see it, nothing of what you want will happen. Bad news, eh?

    Code you wrote is wrong - not because of obvious mistakes, but - cursor's SELECT statement contains 3 columns which you're fetching into a 1 varchar2 variable. 3 can't fit into 1; not that way, that is.


    Moreover, what would you do with a loop within the function? It can be done, of course, for example (switching to cursor FOR loop for simplicity), but - depending on where you put RETURN, you'll either return the first O_RESULT value or the last (see comments within the code):

    for cur_r in (select sprhold_hldd_code, ...
                  from sprhold ...
                  where --> ID condition missing here; ID you're passing, allegedly
                 )
    loop
      if cur_r.sprhold_hldd_code in ('TL', 'TY', ...) then ...
         -- in a number of IFs, you find what O_RESULT variable is
      end if;
    
      -- if you put RETURN here, only one loop iteration will execute
    
    end loop;
    
    -- if you put RETURN here, only the last O_RESULT value will be returned
    

    It means that you'd actually want to put a loop OUTSIDE of the function, i.e. call the function in a loop for all those IDs you're about to pass to the function. Something like this:

    function f_result (par_id in number) return varchar2 is
      o_result varchar2(20);
    begin
      select sprhold_hldd_code
        into l_sprhold_hldd_code
        from sprhold ...
        where some_id = par_id;
    
      if l_sprhold_hldd_code in ...
         -- find O_RESULT in a number of IFs
      end if;
    
      return o_result;
    end;
    

    Now call it in a loop

    begin
      for cur_r in (select id from some_table where some_condition) loop
        dbms_output.put_line('For ID = ' || cur_r.id || ', function returned ' || f_result(cur_r.id));
      end loop;
    end;
    

    If none of above helps, try to rephrase the question.

提交回复
热议问题