1

我的 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

有什么想法或建议吗?

4

1 回答 1

0
  WHEN TABLE_MISSING
      THEN
        NULL;

Looks like an error that you should raise to the caller and fail your process. "Table missing" is not an error you'd want to ignore.

  WHEN OTHERS THEN
         NULL;

Another really bad thing to do.Unless your process has no meaning and no one cares if it works.

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

Now as for the actual fix, if the table might or might not exists during run time, you'll need to use dynamic sql to do all your operations. To check if it exists or not, you can use the data dictionary views (user_tables, all_tables or dba_tables).

于 2012-09-17T21:45:43.283 回答