如何在if()条件oracle pl-sql中添加compare select语句

时间:2014-08-28 12:06:05

标签: sql oracle plsql

如何在Oracle pl-sql

中的if()条件中添加compare select语句
IF( (v_c1.int_rate = '0') 
    AND (local_type_advances in (SELECT DISTINCT(REF_CODE) 
                                 FROM XYZ 
                                 WHERE REF_REC_TYPE = '39' AND DEL_FLG = 'N'         
                                   AND REF_CODE BETWEEN '1' AND '50' )))

其中select查询生成以下输出

'31','32','33'

现在如下所示,如果我对其进行硬编码,那么它可以正常工作,但在查询中无法正常工作

IF( (v_c1.int_rate='0') AND (local_type_advances in ('31','32','33')))

3 个答案:

答案 0 :(得分:2)

我所做的是,我已将localvariable中的选择状态的结果存储为varchar2。并添加了这个局部varchar,如果条件中的条件是结果而不是那个select statment

select '''' || listagg(REF_CODE, ''',''' ) within group (order by REF_CODE) || '''' as type_advances into local_type_advances_list from RCT@ADF2FIN where REF_REC_TYPE = '39' AND DEL_FLG = 'N' AND REF_CODE BETWEEN '1' AND '50';


IF( (v_c1.int_rate='0') AND (local_type_advances in (local_type_advances_list)) )

答案 1 :(得分:0)

由于您无法在IN子句中使用select语句,因此将其分为两部分。选择搜索变量所需的范围,然后搜索该变量以查看该值是否在那里。下面是一个使用LISTAGG()将逗号分隔列表返回到变量和REGEXP_LIKE以测试变量中的列表以查看值是否在列表中的工作示例。 X_TBL有一列,X_COL包含三行,' 31'' 32'和! 33'分别

declare
  local_type_advance_string varchar2(50);        -- Holds the list returned from 
                                                 --   the query.
  local_type_advance_nbr    varchar2(2) := '32'; -- Value to test the list for.
begin

  --  Use listagg() to select a comma-separated list into a string variable.
  --  X_TBL is the table, X_COL is the column.
  SELECT LISTAGG(X_COL, ',') WITHIN GROUP( ORDER BY X_COL)
  into local_type_advance_string
  FROM X_TBL;

  --dbms_output.put_line(local_type_advance_string);

  IF regexp_like(local_type_advance_string, '(^|\W)'|| 
                 local_type_advance_nbr || '(\W|$)') then
    dbms_output.put_line(local_type_advance_nbr || ' Is in there!');
  else
    dbms_output.put_line(local_type_advance_nbr || ' Is not found');
  end if;

end;

正则表达式允许搜索号的位置,无论它位于列表的开头,中间还是末尾。

通过Toad运行:

32 Is in there!

答案 2 :(得分:0)

即使你可以把那个大的'select放在那里,我也建议保持if语句的简单性以便于维护。这有用并且更清晰,特别是如果你添加合理的评论:

create table XYZ(
  REF_REC_TYPE  int,
  DEL_FLG       char( 1 ),
  REF_CODE      int
);

insert into XYZ
select 39, 'Y', 28 from dual union all
select 39, 'Y', 29 from dual union all
select 39, 'Y', 30 from dual union all
select 39, 'N', 31 from dual union all
select 39, 'N', 32 from dual union all
select 39, 'N', 33 from dual union all
select 39, 'Y', 34 from dual union all
select 39, 'Y', 35 from dual union all
select 39, 'Y', 36 from dual;

DECLARE
  int_rate            INT := 0;
  local_type_advances INT := 32;
  found               INT;
BEGIN
  -- See if we have any whatever
  SELECT  count(*)
  INTO    found
  FROM    XYZ 
  WHERE   REF_REC_TYPE = 39
    AND   DEL_FLG = 'N'         
    AND   REF_CODE = local_type_advances;

  IF int_rate = 0
     AND found > 0 THEN
        dbms_output.put_line( 'True: ' || found );
    ELSE
        dbms_output.put_line( 'False: ' || found );
    END IF;
END;

顺便说一句,由于某种原因,这不会在SqlFiddle中执行。碰巧的是,我发现一个非常好的Oracle DB坐在抽屉里,放在一些旧餐巾纸下面。所以我可以验证这是好的代码。