【问题标题】:How to add exception in the oracle statementoracle语句中如何添加异常
【发布时间】:2015-07-30 05:08:05
【问题描述】:

我有以下代码

CREATE PROCEDURE get_details (
  start_time IN  DATE,
  End_Time   IN  DATE,
  o_results1 OUT SYS_REFCURSOR,
  o_results2 OUT SYS_REFCURSOR,
  o_results3 OUT SYS_REFCURSOR,
  o_results4 OUT SYS_REFCURSOR,
  o_results5 OUT SYS_REFCURSOR,
  o_results6 OUT SYS_REFCURSOR
)
AS

BEGIN

  OPEN o_results1 FOR
  SELECT DESCRIPTION, ACTION_BY FROM table1 WHERE  date_time BETWEEN start_time AND End_Time;

  EXCEPTION
   WHEN NO_DATA_FOUND THEN 
   DBMS_OUTPUT.PUT_LINE('NO DATA EXISTS IN TABLE1');  

  OPEN o_results2 FOR
  SELECT *
  FROM   table2
  WHERE  date_time BETWEEN start_time AND End_Time;

  OPEN o_results3 FOR
  SELECT *
  FROM   table3
  WHERE  date_time BETWEEN start_time AND End_Time;

  OPEN o_results4 FOR
  SELECT *
  FROM   table4
  WHERE  date_time BETWEEN start_time AND End_Time;

  OPEN o_results5 FOR
  SELECT *
  FROM   table5
  WHERE  date_time BETWEEN start_time AND End_Time;

  OPEN o_results6 FOR
  SELECT *
  FROM   table6
  WHERE  date_time BETWEEN start_time AND End_Time;
END;
/

当我查询以下内容时,我想要输出table1中没有数据

VARIABLE o_result1 REFCURSOR; 
VARIABLE o_result2 REFCURSOR; 
VARIABLE o_result3 REFCURSOR; 
VARIABLE o_result4 REFCURSOR; 
VARIABLE o_result5 REFCURSOR; 
VARIABLE o_result6 REFCURSOR;

EXECUTE get_details( TO_DATE('16-Jul-2015 04:00:10', 'DD-MON-YYYY HH24:MI:SS'), TO_DATE('16-Jul-2015 20:14:16', 'DD-MON-YYYY HH24:MI:SS'), :o_result1, :o_result2, :o_result3, :o_result4, :o_result5, :o_result6 ); 

PRINT o_result1; 

这里,date_time 是存储日期和时间的列。 我收到一个错误。这是为什么?请注意,我是 Oracle 程序编写的新手。

错误:

ORA-24338: statement handle not executed 24338. 00000 - "statement handle not executed" 
*Cause: A fetch or describe was attempted before executing a statement handle.     
*Action: Execute a statement and then fetch or describe the data. 

【问题讨论】:

  • 你能发布你得到的错误吗?
  • ORA-24338: statement handle not executed 24338. 00000 - "statement handle not executed" *Cause: A fetch or describe was attempted before executing a statement handle. *Action: Execute a statement and then fetch or describe the data.
  • 只要去掉异常语句,整个事情就完美了。

标签: oracle plsql


【解决方案1】:

在 begin.. end 中使用 begin .. end 如下所示

BEGIN 
BEGIN
<query>
<exception>
END;

<query>
<exception>


END;
/

When should I nest PL/SQL BEGIN...END blocks?寻求帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-17
    • 2015-07-19
    • 1970-01-01
    • 2017-06-04
    • 1970-01-01
    • 2017-03-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多