7

我有一组表名,比如说 150 个。每个表都有mail_id列,现在我想mail_id在所有表中搜索一个。为此,我写了一个 Plsql 块。当我遍历一组表时,某些表不存在,因此会引发异常。我有异常处理块来处理该异常。现在我想循环整个表,即使它引发异常?任何想法?实际上我的块没有处理那个特殊的异常!

declare
my_mail_id varchar2(50):='xyaksj@jsm.com';
tmp_table varchar2(125);
type varchar_collector is table of varchar2(255);
var varchar_collector;
table_does_not_exist exception;  
PRAGMA EXCEPTION_INIT(table_does_not_exist, -00942);

begin

for cntr in (select table_name from user_tables)
    loop

    tmp_table:=cntr.table_name;
    dbms_output.put_line(tmp_table);
        for mail in (select email_address from tmp_table where lower(email_address) like '%my_mail_id%' )
        loop
            dbms_output.put_line(tmp_table);
        end loop;
    end loop;

    exception 
    when no_data_found then 
        dbms_output.put_line('email address not found');

    WHEN table_does_not_exist then 
         dbms_output.put_line('table dose not exists');

    WHEN OTHERS THEN
    --raise_application_error(-20101, 'Expecting at least 1000 tables');
    IF (SQLCODE = -942) THEN
        --DBMS_Output.Put_Line (SQLERRM);
        DBMS_Output.Put_Line ('in exception');--this exception not handled
    ELSE
        RAISE;
    END IF;
end;
4

3 回答 3

4

只需在循环内的匿名块中处理您的异常。

DECLARE
  my_mail_id VARCHAR2(50) := 'xyaksj@jsm.com';
  tmp_table  VARCHAR2(125);
  TYPE varchar_collector IS TABLE OF VARCHAR2(255);
  var varchar_collector;
  table_does_not_exist EXCEPTION;
  PRAGMA EXCEPTION_INIT(table_does_not_exist, -00942);

BEGIN

  FOR cntr IN (SELECT table_name FROM user_tables)
  LOOP
    BEGIN
      tmp_table := cntr.table_name;
      dbms_output.put_line(tmp_table);
      FOR mail IN (SELECT email_address
                     FROM tmp_table
                    WHERE lower(email_address) LIKE '%my_mail_id%')
      LOOP
        dbms_output.put_line(tmp_table);
      END LOOP;

    EXCEPTION
      WHEN no_data_found THEN
        dbms_output.put_line('email address not found');

      WHEN table_does_not_exist THEN
        dbms_output.put_line('table dose not exists');

      WHEN OTHERS THEN
        --raise_application_error(-20101, 'Expecting at least 1000 tables');
        IF (SQLCODE = -942)
        THEN
          --DBMS_Output.Put_Line (SQLERRM);
          DBMS_Output.Put_Line('in exception'); --this exception not handled
        ELSE
          RAISE;
        END IF;
    END;
  END LOOP;
END;
于 2018-04-20T12:14:29.143 回答
1

如果您从 user_tables 中选择并发现其中一些不存在,那么您可能正在尝试查询回收站中的表(它们的名称以 BIN$ 开头)。

如果是这样,请将您的查询更改为:

select table_name
from   user_tables
where  dropped = 'NO';

您应该将第二个游标替换为调用立即执行,通过在 table_name 中连接来构造查询,而不仅仅是使用变量作为表名,您也可以将查询构造为:

select count(*)
from   table_name
where  lower(email_address) like '%my_mail_id%'
and    rownum = 1;

这样,您将检索一个 0 或 1 的记录,以指示是否找到了电子邮件地址,并且无需进行错误处理。

于 2013-05-29T08:16:09.467 回答
-1
try below code...

DECLARE
   foo  BOOLEAN;
BEGIN
   FOR i IN 1..10 LOOP
      IF foo THEN
         GOTO end_loop;
      END IF;
   <<end_loop>>  -- not allowed unless an executable statement follows
   NULL; -- add NULL statement to avoid error
   END LOOP;  -- raises an error without the previous NULL
END;
于 2013-05-29T08:11:39.857 回答