oracle plsql复杂语句里面if条件

时间:2016-06-29 06:08:02

标签: oracle plsql

在pl / sql代码中,我有一个变量,它从特定员工的表中获取designation_code。

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;

2 个答案:

答案 0 :(得分:1)

我会用这种方式编写它,使用exists来避免额外的扫描,并使用count来避免异常处理。

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;

答案 1 :(得分: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;