简体   繁体   中英

IF ELSE statement inside another IF ELSE

I have part of my procedure something like this:

     declare 
         v_cnt_1  number;
         v_cnt_2 number;
    begin
        with input1  as(
        select .........
        from...
        where.....)
        select count(*) into v_cnt_1  
        from input1  t
        where......);

        with input2  as(
        select .........
        from...
        where.....)
        select count(*) into v_cnt_2 
        from input2  t
        where......);

       IF v_cnt_1 >0 or v_cnt_2 >0
      THEN DBMS_OUTPUT.PUT_LINE('all set');
      ELSE  DBMS_OUTPUT.PUT_LINE('take further action');
       end if;
       end;

my goal here is :if this query returns the result of 'take further action' then i have to implement other steps which if further if/else statement. I have four more situation(if/else) to add if this returns 'take further action'. how can i add if/else on the basis of this output? Or do i need to create another procedure and call this procedure inside new procedure?

    declare 
             v_cnt_1  number;
             v_cnt_2 number;
             Take_further_action  boolean:=False;
        begin
            with input1  as(
            select .........
            from...
            where.....)
            select count(*) into v_cnt_1  
            from input1  t
            where......);

            with input2  as(
            select .........
            from...
            where.....)
            select count(*) into v_cnt_2 
            from input2  t
            where......);

           IF v_cnt_1 >0 or v_cnt_2 >0
          THEN DBMS_OUTPUT.PUT_LINE('all set');
          ELSE  Take_futher_action :=True;
                 v_cnt_1 :=0;
                 v_cnt_2 :=0;
    --DBMS_OUTPUT.PUT_LINE('take further action');
           end if;
    --Now put your select into statments here
    IF (v_cnt_1 >0 or v_cnt_2 >0) and Take_further_action  
          THEN DBMS_OUTPUT.PUT_LINE('all set');
Take_further_action  :=False;
          ELSE  Take_further_action  :=True;
                 v_cnt_1 :=0;
                 v_cnt_2 :=0;
    --DBMS_OUTPUT.PUT_LINE('take further action');
           end if;

    --Now again put your select into statments here
    IF (v_cnt_1 >0 or v_cnt_2 >0) and Take_further_action  
          THEN DBMS_OUTPUT.PUT_LINE('all set');
Take_further_action  :=False;
          ELSE  Take_further_action  :=True;
                 v_cnt_1 :=0;
                 v_cnt_2 :=0;

           end if;
       --You can perform any number of check with if-then-else as per required

           end;

"if this query returns the result of 'take further action' then i have to implement other steps which if further if/else statement. "

We can nest IF statements. You don't say what your further statements are but you code might look like this:

...
IF v_cnt_1 >0 or v_cnt_2 >0
    THEN DBMS_OUTPUT.PUT_LINE('all set');
ELSE  
    --  take further action
    if whatever = 0  then
        DBMS_OUTPUT.PUT_LINE('do something #1');
    elsif whatever > 0 and yeahyeah = 0 then
        DBMS_OUTPUT.PUT_LINE('do something #2');
    elsif whatever > 0 and yeahyeah > 0 then
        DBMS_OUTPUT.PUT_LINE('do something #3');
   end if;
end if;

If this is what you need you could also structure it as a CASE statement:

case
   when v_cnt_1 >0 or v_cnt_2 >0 then
        DBMS_OUTPUT.PUT_LINE('all set');
   when whatever = 0  then
        DBMS_OUTPUT.PUT_LINE('do something #1');
   when whatever > 0 and yeahyeah = 0 then
        DBMS_OUTPUT.PUT_LINE('do something #2');
   when whatever > 0 and yeahyeah > 0 then
        DBMS_OUTPUT.PUT_LINE('do something #3');
   else
        DBMS_OUTPUT.PUT_LINE('Unexpected state');
   end case;

Note that CASE and ID/ELSIF evaluations will short-circuit. That means the first the program executes the first matching condition, so we need to have the specific case before the general one. This is no good:

 case 
     when whatever = 0  and yeahyeah > 0 then
         dbms_output.put_line('do something');
     when whatever = 0 and yeahyeah = 1 then
         dbms_output.put_line('will never execute');

"do i need to create another procedure and call this procedure inside new procedure"

Not sure if what your question drives at but if the executed steps are complicated (say more than a couple of lines of code) it's cleaner to call procedures - they can be local - because it's just easier to read the whole program. In skeleton code that would look like:

declare
    v_cnt_1  number;
    v_cnt_2 number;
    ...
    procedure proc1(p1 number) is 
    ...
    end p1;

    procedure proc2(p1 number) is 
    ...
    end p2;
begin
    ...
    case
    when v_cnt_1 >0 or v_cnt_2 >0 then
        null -- 'all set';
   when whatever = 0  then
        proc1(v_cnt_1);
   when whatever > 0 and yeahyeah = 0 then
        proc1(v_cnt_2);
   when whatever > 0 and yeahyeah > 0 then
        proc1(v_cnt_1);
        proc2(v_cnt_2);
   else
       proc3(42);
   end case;

This way it is easy to grok the whole case statement and see which condition triggers which action. Of course, giving the procedures meaningful names helps this understanding (not always easy with the Oracle naming limit of thirty characters).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM