PL-SQL条件中使用的单例查询(IF语句)

时间:2016-01-05 18:06:34

标签: oracle plsql

尝试运行我的代码时出现以下错误:

BEGIN
IF((select count(*) from tablename where column = 'value') > 0) THEN
-- do stuff.
ELSE
-- do stuff.
END IF;
END;

错误:

ORA-06550: line 2, column 5:
PLS-00103: Encountered the symbol "SELECT" when expecting one of the following:

   ( - + case mod new not null <an identifier>
   <a double-quoted delimited-identifier> <a bind variable>
   continue avg count current exists max min prior sql stddev
   sum variance execute forall merge time timestamp interval
   date <a string literal with character set specification>
   <a number> <a single-quoted SQL string> pipe
   <an alternatively-quoted string literal with character set specification>

显然错误代码毫无意义。我在使用Oracle时已经习惯了这一点。

PL中存在COUNT()函数。还有什么呢?

1 个答案:

答案 0 :(得分:2)

Lance Link是你的掩护吗?您无法在IF语句中进行查询。首先将查询作为选择INTO变量,然后测试变量:

DECLARE
  v_count number;
BEGIN
  select count(*) 
  into v_count
  from tablename 
  where column = 'value';

  IF ( v_count > 0) THEN
    -- do stuff.
  ELSE
    -- do stuff.
  END IF;
END;

给Mata我的问候。