2016-06-29 13 views
0

pl/sqlコードでは、特定の従業員の表からdesignation_codeを取得する変数があります。oracle plsql complex statement in if条件

STMT#1

select basic_designation into source_designation 
    from tbl_emp_basic_profile where basic_id=source_id; 

今私はsource_designationがコードやないの集合であるかどうかを確認する必要があります。 {コードのセット}は以下のSQLによって生成することができる。

STMT#2

select distinct(BASIC_DESIGNATION) as "SET_OF_CODES" 
    from TBL_EMP_BASIC_PROFILE 
where BASIC_DESIGNATION in (select to_number(SD_DESIGNATION_CODE) 
         from TBL_SETTINGS_DESIGNATION 
         where lower(SD_DESIGNATION_NAME) like '%professor%' 
         or lower(SD_DESIGNATION_NAME) like '%lecturer%'); 

私はそれをどのように行いますか?次のようなIF文を書くことができますか?

IF(source_designation in (STMT#2)) then 
    --do somtehing 
END IF; 
+0

なぜあなたの 'WHERE'節に条件を追加しないのですか?このように: 'select v_exists from ... where ... AND BASIC_DESIGNATION = source_designation'。 'v_exists'は見つからなければ0を返し、見つかったものに基づいて0より大きい値を返します。 –

+0

yah今、私は第2ステートメントを第1ステートメントの 'where'節として追加しました。おかげでそれは今働く。 –

答えて

0

このようにすることができます。

1)コレクション を宣言する2)すべての値をsqlのコレクションにフェッチする。 3)値が収集されているかどうかを確認します。

例。

declare 
    type T_SET_OF_CODES is table of varchar2(xxx); -- where xxx is appropriate size of varchar2 for size_of_codes 
    V_SET_OF_CODES T_SET_OF_CODE; 
    begin 


    select distinct(BASIC_DESIGNATION) as "SET_OF_CODES" bulk collect into V_SET_OF_CODES 
     from TBL_EMP_BASIC_PROFILE 
    where BASIC_DESIGNATION in (select to_number(SD_DESIGNATION_CODE) 
          from TBL_SETTINGS_DESIGNATION 
          where lower(SD_DESIGNATION_NAME) like '%professor%' 
          or lower(SD_DESIGNATION_NAME) like '%lecturer%'); 

    IF source_designation member of V_SET_OF_CODES then 
     --do somtehing    
    END IF; 

    end; 
1

これは、余分なスキャニングを避けるために存在を使用し、例外処理を避けるためにカウントを使用します。

select count(1) 
    into designation_is_in_set 
    from dual 
where exists (select 1 from TBL_SETTINGS_DESIGNATION 
where to_number(SD_DESIGNATION_CODE)=source_designation 
    and (
     lower(SD_DESIGNATION_NAME) like '%professor%' 
    or lower(SD_DESIGNATION_NAME) like '%lecturer%' 
     ) 
); 
if designation_is_in_set=1 then 
    -- the des is in the set 
else 
    -- the des is not in the set 
end if;