检查时间是否为真运行查询

时间:2019-08-26 06:27:28

标签: sql oracle

我正在尝试在Oracle DB中运行特定查询:

select
case substr(to_char(sysdate,'hh24:mi'),1,4)
when '11:0' then (select * from <Table name>)
else (something)
end from dual;

每次运行此命令时,都会得到“太多值”,为什么不能解决这个问题?

谢谢。

3 个答案:

答案 0 :(得分:1)

像这样

select * from ...
where substr(to_char(sysdate,'hh24:mi'),1,4)='11:0'

答案 1 :(得分:0)

Oracle无法将子查询中的多列/多行数据显示到单个列中。

select
case substr(to_char(sysdate,'hh24:mi'),1,4)
when '11:0' then (select * from <Table name>) -- what if this query returns two rows/columns?
else (something)
end from dual;

您可以在此类子查询上使用aggregate function,或者如果输出中需要多个值,只需使用CASE..WHEN即可。

-聚合函数以获取一行和一列:

select
case substr(to_char(sysdate,'hh24:mi'),1,4)
when '11:0' then (select count(1) from <Table name>) -- aggregate function without group by 
else (something)
end from dual;

-使用CASE..WHEN

select
case substr(to_char(sysdate,'hh24:mi'),1,4)
when '11:0' then <col_1 from table name> 
else (something)
end as val1,
case substr(to_char(sysdate,'hh24:mi'),1,4)
when '11:0' then <col_2 from table name> 
else (something)
end as val2,
..
..
from <Table name>;

干杯!

答案 2 :(得分:0)

@Tejash完美地解释了它。

这是另一种方法,“玩”它:

set serveroutput on
DECLARE
   l_hour VARCHAR2(10);
   l_col1 VARCHAR2(100);
   l_col2 VARCHAR2(100);
BEGIN
   WHILE true 
   LOOP
      SELECT TO_CHAR(SYSDATE, 'hh24:mi') INTO l_hour FROM dual;
      IF l_hour = '11:00'
      THEN
         select <col1 name>, <col2 name> into l_col1, l_col2
         from <Table name>;

         dbms_output.put_line(l_col1);
         dbms_output.put_line(l_col2);
      END IF;
    END LOOP;  
END;
/