2017-11-26 2 views
0

나는 이런 내 절차 무언가의 일부가 다른 IF 내부 ELSE 문 :IF ELSE

 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; 

내 목표는 여기에 있습니다 :이 쿼리의 결과를 반환하는 경우 다음 내가 기타를 구현해야 '추가 조치를 취할 수' 단계 if/else 문. 이 경우 '추가 작업 수행'을 반환 할 경우 추가 할 상황이 4 가지 더 있습니다 (if/else). 이 출력에 따라 if/else를 어떻게 추가 할 수 있습니까? 아니면 다른 프로 시저를 만들고 새 프로 시저 안에서이 프로 시저를 호출해야합니까?

답변

0
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; 
+0

IF (v_cnt_1> 0 또는 v_cnt_2> 0) 및 Take_further_action을 두 번 사용해야하는 이유 THEN DBMS_OUTPUT.PUT_LINE (​​'all set'); – kkl

+0

예 KKl 만 Take_further_action을 사용하여 다른 조건으로 진행할 수 있습니다. 그러나 논리적으로 그것은 또한 정확하다. –

0

는 "이 쿼리의 결과를 반환하는 경우 '추가 조치를 취할 수'그럼 내가 다른 단계를 구현해야하는 경우 추가하면/else 문."우리는 둥지 수

문 IF. 당신은 당신의 더 문장이 무엇인지 말을하지 않습니다하지만 당신의 코드는 다음과 같습니다이 당신이 또한 CASE 문으로 구조화 할 수 필요한 경우

... 
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; 

을 :

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; 

참고 CASE 및 ID/ELSIF 평가가 단락 될 것입니다. 즉 첫 번째 프로그램이 첫 번째 일치 조건을 실행한다는 의미이므로 일반 사례 앞에 특정 사례가 있어야합니다. 이것은 좋은 없습니다 :

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'); 

경우

확실하지 "나는 새 프로 시저 내부 절차를 다른 프로 시저를 작성하고 호출해야 할"무슨 그러나 실행 단계는 경우 귀하의 질문에 드라이브 복잡한 코드 (말하자면 2 줄 이상의 코드)는 전체 프로그램을 읽기가 쉽기 때문에 프로 시저를 호출하는 것이 더 깨끗합니다. 골격 코드에서 그 보일 것 같은 :

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; 

는 전체 case 문을 grok 수 및 어떤 행동 조건 트리거를 쉽게 볼 수 이쪽으로. 물론 프로 시저의 의미있는 이름을 지정하면이 이해에 도움이됩니다 (오라클 이름 제한 인 30 자로 항상 쉬운 것은 아닙니다).