[英]how handle table or view does not exist exception?
I have a set of table names, let say 150. Each table have mail_id
column, now I want to search one mail_id
in all of the table. 我有一组表名,比方说150.每个表都有
mail_id
列,现在我想在所有表中搜索一个mail_id
。 For that I wrote one Plsql block. 为此我写了一个Plsql块。 When I loop through the set of table some tables do not exists so it raises an exception.
当我遍历表集时,一些表不存在,因此它引发异常。 I have exception handling block to handle that exception.
我有异常处理块来处理该异常。 Now I want to loop entire table even though it raise an exception?
现在我想循环整个表,即使它引发异常? Any idea?
任何的想法? Actually my block didn't handle that particular exception!
实际上我的块没有处理那个特殊的异常!
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;
Just handle your exceptions in anonymous block inside the loop. 只需在循环内的匿名块中处理异常。
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;
If you're selecting from user_tables and finding that some of them do not exist then you're probably trying to query tables that are in the recycle bin (their names begin BIN$). 如果您从user_tables中选择并发现其中一些不存在,那么您可能正在尝试查询回收站中的表(它们的名称以BIN $开头)。
If so, change your query to: 如果是这样,请将您的查询更改为:
select table_name
from user_tables
where dropped = 'NO';
You should replace your second cursor with a call to execute immediate also, constructing the query by concatenating in the table_name not just using a variable as the table name, and you might as well construct the query as: 您应该将第二个游标替换为执行立即执行的调用,通过在table_name中连接而不仅仅使用变量作为表名来构造查询,您也可以将查询构造为:
select count(*)
from table_name
where lower(email_address) like '%my_mail_id%'
and rownum = 1;
That way you'll retrieve a single record that is either 0 or 1 to indicate whether the email address was found, and no need for error handling. 这样,您将检索单个记录,该记录为0或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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.