2011-12-01 1 views
0

첫 번째 쿼리가 null 값을 반환하거나 아무런 레코드도 반환하지 않으면 프로 시저에서 두 번째 쿼리는 두 번째 쿼리도 실행하면 null 값이 반환되거나 레코드가 반환되지 않으므로 기본값을 반환해야합니다. 이 절차를 만드는 방법? else 문이나 예외 처리기를 사용해야합니까?첫 번째 쿼리가 null 값을 반환하면 내 두 번째 쿼리도 실행해야합니다. 두 번째 쿼리는 null로 표시되고 내 기본값은 표시된대로

create or replace function get_queue_id 
    (p_product_code in mo_product_master.product_code%type 
    , p_intermediary_code in intrfc_intermediary_mstr_view.intermediary_code%type) 
    return mo_product_master.queue_id%type 
as 
    return_value number; 
begin 
    -- preferred_choice 
    begin 
     select pm.queue_id 
     into return_value 
     from mo_product_master pm 
     where pm.product_code=p_product_code 
    exception 
     when no_data_found then 
      null; 
    end; 

    if return_value is null then 
     -- second_choice 
     begin 
      select qim.queue_id 
      into return_value 
      from mo_queue_inter_map_master qim 
       , intrfc_intermediary_mstr_view imv 
      where qim.category_code =imv.category_code 
      and imv.intermediary_code=p_intermediary_code; 
     exception 
      when no_data_found then 
       null; 
     end; 

     if return_value is null then 
      -- default_value 
      select id 
      into return_value 
      from mo_queue_master 
      where queue_name='others' 
      and status='Active'; 
     end if; 
    end if; 
    return return_value; 
end; 
/

그것은 조금 투박하지만이 작업을 수행합니다

답변

2

한 가지 방법 같은 쿼리가 중첩 될 물품.

NO_DATA_FOUND 예외를 억제하는 것은 일반적으로 권장되는 방법은 아니지만이 시나리오에 가장 적합하다고 생각합니다. 첫 번째 QUEUE_ID를 찾는 것이 처리해야하는 예외가 아닌 일반 비즈니스 논리의 일부입니다. 예외 처리기에서 후속 선택을 중첩하는 것이 비즈니스 규칙을 거의 표현할 수 있다고 생각하지 않습니다.

+0

고마워요. 당신의 코드가 나를 도왔습니다. – leelavinodh

0

는 문이 같은이 경우이 일이

select * from tablename 
    where field1 in nvl(select field1 from table2 ,'defaultvalue') 
+0

내가이 오류 ORA-00936를 얻고있다 :에서 product_code를 및 pm.queue_id (SELECT NULL입니다 :이 쿼리를 실행하려고하는 동안 표현 누락 SELECT는 pm.product_code =이 mo_product_master의 오후 로부터 을 pm.queue_id IMV intrfc_intermediary_mstr_view mo_queue_inter_map_master의 QIM FROM qim.queue_id , QIM.CATEGORY_CODE가 = imv.category_code AND imv.intermediary_code = mo_queue_master에서 intermediary_code, 선택 대기열 이름 ID = '다른'상태 =) '활성'; – leelavinodh

+0

oracle에는 isnull() 함수가 없습니다. isull 위치에 nvl()을 사용해야합니다. –

+0

아직 내 원하는 결과를 얻지 못했습니다. My SQL 문 MyFirstQuery는 입니다. select pm.queue_id from mo_product_master pm pm_product_code = : product_code ; 내 초 쿼리가 레코드를 반환하지 않으면 두 번째 쿼리 출력이 표시되어야합니다. SELECT qim.queue_id FROM mo_queue_inter_map_master QIM, intrfc_intermediary_mstr_view IMV WHERE QIM.CATEGORY_CODE = imv.category_code 및 imv.intermediary_code = : intermediary_code; 이전 쿼리가 레코드를 반환하지 못하면 세 번째 쿼리 출력이 표시되어야합니다. mo_queue_master에서 select id, QUEUE_NAME = 'others'및 status = 'Active'. – leelavinodh

관련 문제