shell中的表或视图不存在异常

时间:2012-09-17 19:45:03

标签: exception view plsql block

在我的shell脚本中从pl / sql块出现此问题。这是脚本 PL / SQL BLOCK:

$ORACLE_HOME/bin/sqlplus -s system/${SysPwd}@${SList} <<!
--WHENEVER SQLERROR EXIT FAILURE;
  SET SERVEROUTPUT ON SIZE UNLIMITED;
  SET FEEDBACK OFF;
  SET DEFINE OFF;
  spool ${RESULT} append
DECLARE
TABLE_MISSING EXCEPTION;
  PRAGMA EXCEPTION_INIT(TABLE_MISSING,-6550);
    err_num NUMBER;
    err_msg VARCHAR2(100);
    cnt number;
    sql_stmt varchar2(2000);
    v_user varchar2(100) := '${User}';
    p_username varchar2(100);
    p_fullname varchar2(100);
    p_account_state char(2);

BEGIN
  select count(*) into cnt from users where upper(full_name) like '%${PaceUser}%';
  if cnt <> 0 then
  select rtrim(username),rtrim(full_name),account_state into p_username,p_fullname,p_account_state from users where upper(full_name) like '%${User}';
   dbms_output.put_line('User ${User} exist in the database ${SList}.....');
  end if;
EXCEPTION
  when NO_DATA_FOUND then
  NULL;
  WHEN TABLE_MISSING
      THEN
        NULL;
  WHEN OTHERS THEN
         NULL;
END;
/

和错误:

ERROR:


   select count(*) into cnt from users where upper(full_name) like '%TYLER MACHUTCHON%';
                                      *
ERROR at line 16:
ORA-06550: line 16, column 48:
PL/SQL: ORA-00942: table or view does not exist
ORA-06550: line 16, column 3:
PL/SQL: SQL Statement ignored
ORA-06550: line 18, column 120:
PL/SQL: ORA-00942: table or view does not exist
ORA-06550: line 18, column 3:
PL/SQL: SQL Statement ignored

有任何想法或建议吗?

1 个答案:

答案 0 :(得分:0)

  WHEN TABLE_MISSING
      THEN
        NULL;

看起来您应该向调用者提出错误并使您的进程失败。 “缺少表格”不是您想要忽略的错误。

  WHEN OTHERS THEN
         NULL;

另一个非常糟糕的事情。除非你的过程没有任何意义,否则无人问津。

=============================================== ==================================

现在,对于实际修复,如果在运行时期间表可能存在或可能不存在,则需要使用动态sql来执行所有操作。要检查它是否存在,您可以使用数据字典视图(user_tables,all_tables或dba_tables)。