异常处理后继续执行

问题描述 投票:0回答:3

我正在编写一个脚本来执行多个唯一查询(不能放在循环中)。添加异常处理程序时,一旦发生第一个错误,便会处理异常,但编译器会退出该块并终止程序。

如何使我的程序处理异常并继续执行下一个查询?

下面是我一直在使用的代码:

DECLARE
  NEW_VAR1 VARCHAR2(20000);
  table_does_not_exist exception;
  PRAGMA EXCEPTION_INIT(table_does_not_exist, -942);
BEGIN
  DBMS_output.put_line('Query 1 Execution');
  EXECUTE IMMEDIATE 'BEGIN
    SELECT "defaultpwd" INTO :out1 from sys.user$ where name="APEX_040000" and substr(spare4,3,40)=rawtohex(utl_raw.cast_to_varchar2(sys.dbms_crypto.hash(utl_raw.cast_to_raw("oracle")||hextoraw(substr(spare4,43,20)), 3))) UNION SELECT "defaultpwd" from sys.user$ where name="APEX_040000" and password="EE7785338B8FFE3D";
  END;'
  USING out NEW_VAR1;
  DBMS_output.put_line(NEW_VAR1);

  DBMS_output.put_line('Query 2 Execution');
  EXECUTE IMMEDIATE 'BEGIN
    SELECT "defaultpwd" INTO :out2 from sys.user$ where name="APEX_040000" and substr(spare4,3,40)=rawtohex(utl_raw.cast_to_varchar2(sys.dbms_crypto.hash(utl_raw.cast_to_raw("oracle")||hextoraw(substr(spare4,43,20)), 3))) UNION SELECT "defaultpwd" from sys.user$ where name="APEX_040000" and password="EE7785338B8FFE3D";
  END;'
  USING out NEW_VAR1;
  DBMS_output.put_line(NEW_VAR1);
EXCEPTION
  WHEN table_does_not_exist THEN
    DBMS_output.put_line('Table does not exist!!');
  WHEN others then
    DBMS_output.put_line('Error!!');
END;

在这种情况下,期望的结果是:

Query 1 Execution
Error!!
Query 2 Execution
Error!!

实际结果是:

Query 1 Execution
Error!!

如果上述代码效率不高,我可以选择执行多种不同查询的替代方法。

oracle oracle11g oracle10g
3个回答
1
投票

我认为您需要为每个查询使用不同的异常块。像这样的东西:

    DECLARE
      NEW_VAR1 VARCHAR2(20000);
      table_does_not_exist exception;
      PRAGMA EXCEPTION_INIT(table_does_not_exist, -942);
    BEGIN
      BEGIN
          DBMS_output.put_line('Query 1 Execution');
          EXECUTE IMMEDIATE 'BEGIN
            SELECT "defaultpwd" INTO :out1 from sys.user$ where name="APEX_040000" and    substr(spare4,3,40)=rawtohex(utl_raw.cast_to_varchar2(sys.dbms_crypto.hash(utl_raw.cast_to_raw("oracle")||hextoraw(substr(spare4,43,20)), 3))) UNION SELECT "defaultpwd" from sys.user$ where name="APEX_040000" and password="EE7785338B8FFE3D";
          END;'
          USING out NEW_VAR1;
          DBMS_output.put_line(NEW_VAR1);
      EXCEPTION
          WHEN table_does_not_exist THEN
              DBMS_output.put_line('Table does not exist!!');
          WHEN others then
              DBMS_output.put_line('Error!!');
      END;

      BEGIN
          DBMS_output.put_line('Query 2 Execution');
          EXECUTE IMMEDIATE 'BEGIN
            SELECT "defaultpwd" INTO :out2 from sys.user$ where name="APEX_040000" and substr(spare4,3,40)=rawtohex(utl_raw.cast_to_varchar2(sys.dbms_crypto.hash(utl_raw.cast_to_raw("oracle")||hextoraw(substr(spare4,43,20)), 3))) UNION SELECT "defaultpwd" from sys.user$ where name="APEX_040000" and password="EE7785338B8FFE3D";
          END;'
          USING out NEW_VAR1;
          DBMS_output.put_line(NEW_VAR1);
      EXCEPTION
          WHEN table_does_not_exist THEN
              DBMS_output.put_line('Table does not exist!!');
          WHEN others then
              DBMS_output.put_line('Error!!');
      END;
    END;

1
投票

原则上,您必须这样做:

DECLARE
  NEW_VAR1 VARCHAR2(20000);
  table_does_not_exist exception;
  PRAGMA EXCEPTION_INIT(table_does_not_exist, -942);
BEGIN

BEGIN
  DBMS_output.put_line('Query 1 Execution');
  EXECUTE IMMEDIATE 'BEGIN
    SELECT "defaultpwd" INTO :out1 from sys.user$ where name="APEX_040000" and substr(spare4,3,40)=rawtohex(utl_raw.cast_to_varchar2(sys.dbms_crypto.hash(utl_raw.cast_to_raw("oracle")||hextoraw(substr(spare4,43,20)), 3))) UNION SELECT "defaultpwd" from sys.user$ where name="APEX_040000" and password="EE7785338B8FFE3D";
  END;'
  USING out NEW_VAR1;
  DBMS_output.put_line(NEW_VAR1);
EXCEPTION
  WHEN table_does_not_exist THEN
    DBMS_output.put_line('Table does not exist!!');
  WHEN others then
    DBMS_output.put_line('Error!!');
END;

BEGIN
  DBMS_output.put_line('Query 2 Execution');
  EXECUTE IMMEDIATE 'BEGIN
    SELECT "defaultpwd" INTO :out2 from sys.user$ where name="APEX_040000" and substr(spare4,3,40)=rawtohex(utl_raw.cast_to_varchar2(sys.dbms_crypto.hash(utl_raw.cast_to_raw("oracle")||hextoraw(substr(spare4,43,20)), 3))) UNION SELECT "defaultpwd" from sys.user$ where name="APEX_040000" and password="EE7785338B8FFE3D";
  END;'
  USING out NEW_VAR1;
  DBMS_output.put_line(NEW_VAR1);
EXCEPTION
  WHEN table_does_not_exist THEN
    DBMS_output.put_line('Table does not exist!!');
  WHEN others then
    DBMS_output.put_line('Error!!');
END;

END;

或放入循环中:

BEGIN

FOR i IN 1..2 LOOP
  BEGIN
    DBMS_output.put_line('Query '||i||' Execution');
    EXECUTE IMMEDIATE ...

  EXCEPTION
    WHEN table_does_not_exist THEN
      DBMS_output.put_line('Table does not exist!!');
    WHEN others then
      DBMS_output.put_line('Error!!');
  END;
END LOOP;

END;

但是您的代码有几个错误/缺陷:

  • 为什么要运行两次完全相同的查询?
  • EXECUTE IMMEDIATE要求完全返回one行。由于UNION,这不太可能。
  • 代码必须类似于EXECUTE IMMEDIATE 'SELECT ... FROM ...' INTO NEW_VAR1;
  • 我不认为表sys.user$有任何列defaultpwd(小写)
  • 您应使用WHEN others then DBMS_output.put_line('Error!!' || SQLERRM);查看实际错误,否则将其隐藏。因为您是SQL的初学者,所以情况甚至更糟。
  • 您查询的目的是什么,对我来说没有太大意义。如果您想与PASSWORD进行比较,请简化=<>
  • 即使您的查询有效,也没有理由使用动态SQL。使用简单的SELECT ... INTO NEW_VAR1 FROM ...。不需要EXECUTE IMMEDIATE

0
投票

动态SQL有什么特殊原因吗?您运行的查询中没有dynamic

© www.soinside.com 2019 - 2024. All rights reserved.