加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

如果EXISTS条件不适用于PLSQL

发布时间:2020-12-12 13:56:24 所属栏目:百科 来源:网络整理
导读:当条件为TRUE时,我尝试打印TEXT。选择代码完全正常工作。当我只运行选择代码时显示403值。但是当条件存在时,我必须打印一些文本。以下代码有什么问题? BEGINIF EXISTS(SELECT CE.S_REGNO FROMCOURSEOFFERING COJOIN CO_ENROLMENT CE ON CE.CO_ID = CO.CO_
当条件为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:
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;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读