带有变量的Oracle case语句

时间:2018-04-04 15:37:09

标签: oracle case ora-06550 pls-00103

我有一个要求,如果今天是星期一,则运行一个查询,否则运行另一个查询。为此,我写了下面的查询,但我收到以下错误消息。

DECLARE
   l_today_date VARCHAR2(15) := TO_CHAR(SYSDATE, 'DAY');
 BEGIN
   CASE l_today_date
   WHEN 'MONDAY' THEN
     (SELECT st_time AS SYS_DATE,
       start_time                          AS JOB_START_TIME,
       COALESCE (end_job,'Job Is Running') AS JOB_END_TIME,
       CASE duration_job
         WHEN ' min'
         THEN 'Job is Running'
         ELSE duration_job
       END AS JOB_DURATION,
       CASE duration_job
         WHEN ' min'
         THEN 'Job is Running'
         ELSE 'Complete'
       END AS job_status
     FROM
       (SELECT name,
         st_time,
         ABS(floor(((((st_time - lag(end_time) over (order by end_time desc))*24*60*60)/3600)*3600)/60))
         || ' min' duration_job,
         TO_CHAR(st_time, 'hh24:mi:ss')      AS start_time,
         TO_CHAR(lag(end_time)over(order by end_time desc),'hh24:mi:ss') AS end_job
       FROM sc_stask
       WHERE name IN ( '111 has started' ,'111 has ended' )
       ORDER BY st_time DESC
       )
     WHERE name = '111 has started');
   ELSE
     (SELECT st_time                       AS SYS_DATE,
       start_time                          AS JOB_START_TIME,
       COALESCE (end_job,'Job Is Running') AS JOB_END_TIME,
       CASE duration_job
         WHEN ' min'
         THEN 'Job is Running'
         ELSE duration_job
       END AS JOB_DURATION,
       CASE duration_job
         WHEN ' min'
         THEN 'Job is Running'
         ELSE 'Complete'
       END AS job_status
     FROM
       (SELECT name,
         st_time,
         ABS(floor(((((st_time - lag(end_time)over(order by end_time desc) )*24*60*60)/3600)*3600)/60))
         || ' min' duration_job,
         TO_CHAR(st_time, 'hh24:mi:ss')      AS start_time,
         TO_CHAR(lag(end_time)over(order by end_time desc),'hh24:mi:ss') AS end_job
       FROM sc_stask
       WHERE name                      IN ( '111 has started' ,'111 has completed' )
       AND TO_CHAR(st_time,'DD/MM/YYYY')=TO_CHAR(SYSDATE,'DD/MM/YYYY')
       ORDER BY st_time DESC
       )
     WHERE name = '111 has started'
     );
   END CASE;
   dbms_output.Put_line(l_today_date);
END;

以下是我遇到的错误消息

 Error report -
 ORA-06550: line 6, column 6:
 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>
    <an alternat
 ORA-06550: line 22, column 47:
 PLS-00103: Encountered the symbol "OVER" when expecting one of the following:

    . ( ) , * % & = - + < / > at in is mod remainder not rem =>
    <an exponent (**)> <> or != or ~= >= <= <> and or like like2
    like4 likec as between || member submultiset
 06550. 00000 -  "line %s, column %s:\n%s"
 *Cause:    Usually a PL/SQL compilation error.

有人可以帮我解决这个问题。此外,在每个case语句中运行的查询都可以单独运行。但是当我把它们放回一个时,它们却没有。

2 个答案:

答案 0 :(得分:2)

问题在于括号;你不应该在select语句周围有这些:

DECLARE
   l_today_date VARCHAR2(15) := TO_CHAR(SYSDATE, 'DAY', 'NLS_DATE_LANGUAGE=ENGLISH');
 BEGIN
   CASE l_today_date
   WHEN 'MONDAY' THEN
     SELECT st_time AS SYS_DATE,
...
     WHERE name = '111 has started';
   ELSE
     SELECT st_time                       AS SYS_DATE,
...
     WHERE name = '111 has started';
   END CASE;
   dbms_output.Put_line(l_today_date);
END;
/

我还在to_char()添加了可选的第三个参数,因此请确保日期名称是您正在搜索的名称;否则,如果有人从非英语会话中运行它,那么它将无法正常匹配。

顺便提一下,如果您只使用l_tdoay_date值一次 - 并且最后没有dbms_output调试调用 - 那么您就不需要该变量;您可以将case与函数调用一起使用:

BEGIN
   CASE TO_CHAR(SYSDATE, 'DAY', 'NLS_DATE_LANGUAGE=ENGLISH')
   WHEN 'MONDAY' THEN
...

你可以在这里使用if代替case - 或者只使用一个值进行检查,在本例中使用case可能并没有获得太多收益。

您还可以使用单个查询并将case语句逻辑移动到其where子句中,例如:

   FROM sc_stask
   WHERE name IN ( '111 has started' ,'111 has ended' )
   AND (TO_CHAR(SYSDATE, 'DAY', 'NLS_DATE_LANGUAGE=ENGLISH') = 'MONDAY'
     OR TO_CHAR(st_time,'DD/MM/YYYY')=TO_CHAR(SYSDATE,'DD/MM/YYYY')
   )

虽然我通常将最后一部分作为OR TRUNC(st_time) = TRUNC(SYSDATE)而不是将两者都转换为字符串。无论如何,使用这种方法你可能根本不需要PL / SQL块;如果你确实拥有它,那么@Littlefoot提到你必须选择(或者)查询 INTO

答案 1 :(得分:1)

不是CASE,而是IF

begin
  if to_char(sysdate, 'DAY') = 'MONDAY' then
     select ... query you run on monday;
  else
     select ... query you run otherwise
  end if;
end;

[编辑:为INTO条款添加了示例]

SQL> set serveroutput on
SQL>
SQL> declare
  2    l_cnt number;
  3    l_today varchar2(10) := to_char(sysdate, 'DAY');
  4  begin
  5    if l_today = 'THURSDAY' then
  6       select count(*)
  7         into l_cnt          --> this
  8         from emp
  9         where deptno = 10;
 10    else
 11       select count(*)
 12         into l_cnt
 13         from emp
 14         where deptno <> 10;
 15    end if;
 16
 17    dbms_output.put_line('Today is ' || l_today || ' and count = ' || l_cnt);
 18  end;
 19  /
Today is THURSDAY  and count = 11

PL/SQL procedure successfully completed.

SQL>