【问题标题】:IF EXISTS condition not working with PLSQLIF EXISTS 条件不适用于 PLSQL
【发布时间】:2012-10-24 10:22:48
【问题描述】:

我正在尝试在条件为 TRUE 时打印 TEXT。选择代码工作正常。当我只运行选择代码时,它显示 403 值。但是当条件存在时,我必须打印一些文本。以下代码有什么问题。

BEGIN
IF EXISTS(
SELECT CE.S_REGNO FROM
COURSEOFFERING CO
JOIN CO_ENROLMENT CE
  ON CE.CO_ID = CO.CO_ID
WHERE CE.S_REGNO=403 AND CE.COE_COMPLETIONSTATUS = 'C' AND CO.C_ID = 803
)
THEN
    DBMS_OUTPUT.put_line('YES YOU CAN');
END;

这是错误报告:

Error report:
ORA-06550: line 5, column 1:
PLS-00103: Encountered the symbol "JOIN" when expecting one of the following:

   ) , with group having intersect minus start union where
   connect
06550. 00000 -  "line %s, column %s:\n%s"
*Cause:    Usually a PL/SQL compilation error.
*Action:

【问题讨论】:

    标签: sql oracle if-statement plsql


    【解决方案1】:

    IF EXISTS() 语义不正确。 EXISTS 条件只能在 SQL 语句中使用。所以你可以重写你的 pl/sql 块如下:

    declare
      l_exst number(1);
    begin
      select case 
               when exists(select ce.s_regno 
                             from courseoffering co
                             join co_enrolment ce
                               on ce.co_id = co.co_id
                            where ce.s_regno=403 
                              and ce.coe_completionstatus = 'C' 
                              and ce.c_id = 803
                              and rownum = 1
                            )
               then 1
               else 0
             end  into l_exst
      from dual;
    
      if l_exst = 1 
      then
        DBMS_OUTPUT.put_line('YES YOU CAN');
      else
        DBMS_OUTPUT.put_line('YOU CANNOT'); 
      end if;
    end;
    

    或者您可以简单地使用count 函数来确定查询返回的行数,以及rownum=1 谓词-您只需要知道记录是否存在:

    declare
      l_exst number;
    begin
       select count(*) 
         into l_exst
         from courseoffering co
              join co_enrolment ce
                on ce.co_id = co.co_id
        where ce.s_regno=403 
          and ce.coe_completionstatus = 'C' 
          and ce.c_id = 803
          and rownum = 1;
    
      if l_exst = 0
      then
        DBMS_OUTPUT.put_line('YOU CANNOT');
      else
        DBMS_OUTPUT.put_line('YES YOU CAN');
      end if;
    end;
    

    【讨论】:

    • 感谢 Nicholas 的精彩代码。它非常适合我。
    • “简单地使用计数函数”的效率要低得多。坚持存在/不存在。
    • 为什么exists不能在insert语句中使用?
    • exists 当然可以与insert 语句一起使用。
    【解决方案2】:

    不幸的是,PL/SQL 没有像 SQL Server 这样的IF EXISTS 运算符。但你可以这样做:

    begin
      for x in ( select count(*) cnt
                   from dual 
                  where exists (
                    select 1 from courseoffering co
                      join co_enrolment ce on ce.co_id = co.co_id
                     where ce.s_regno = 403 
                       and ce.coe_completionstatus = 'C' 
                       and co.c_id = 803 ) )
      loop
            if ( x.cnt = 1 ) 
            then
               dbms_output.put_line('exists');
            else 
               dbms_output.put_line('does not exist');
            end if;
      end loop;
    end;
    /
    

    【讨论】:

    • 感谢希尔顿的帮助。是的,我从你的代码中得到了这个想法。但我不知道为什么条件是 FALSE 并且它显示 ELSE 条件。 x.cnt 中的值为 0。
    • 这可能是因为我用常量“C”小写了您的查询,应该是大写。
    猜你喜欢
    • 1970-01-01
    • 2022-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-24
    相关资源
    最近更新 更多