将查询结果与PL / SQL中的整数进行比较

时间:2017-06-07 13:03:34

标签: oracle plsql

我写了以下查询。

  IF ( select count(*) 
        from source_table 
       where id=Code 
         and to_date(start_date, 'dd/mm/yyyy') <= sysdate 
         and to_date(end_date, 'dd/mm/yyyy')>=sysdate)>0 
  THEN
    checkWithinDateTime:=1;
  END IF;

每当我尝试执行我的程序时,它会在条件上显示错误。

enter image description here

1 个答案:

答案 0 :(得分:2)

您无法以这种方式检查查询结果;你需要一个变量来处理查询的结果:

SQL> begin
  2      if (select count(1) from dual) = 1 then
  3          dbms_output.put_line('One');
  4      else
  5          dbms_output.put_line('Not one');
  6      end if;
  7  end;
  8  /
    if (select count(1) from dual) = 1 then
        *
ERROR at line 2:
ORA-06550: line 2, column 9:
PLS-00103: Encountered the symbol "SELECT" when expecting one of the following:[...]

您需要:

SQL> declare
  2      vCount number;
  3  begin
  4      select count(1)
  5      into vCount
  6      from dual;
  7      if vCount = 1 then
  8          dbms_output.put_line('One');
  9      else
 10          dbms_output.put_line('Not one');
 11      end if;
 12  end;
 13  /
One

PL/SQL procedure successfully completed.

SQL>