Exception捕捉异常处理,请求大家帮忙
FOR i IN (1..5) LOOP
如果中间产生异常如何继续循环,而不是碰到异常就退出?
END LOOP;
--------实际应用代码功能如下------------------
declare
str_sql varchar2(2000);
cursor get_obj is
select object_type, object_name from USER_OBJECTS
where status = 'INVALID '
and object_type in( 'PACKAGE BODY ', 'VIEW ');
begin
for rec_ in get_obj loop
if (rec_.object_type = 'VIEW ') then
str_sql := 'ALTER VIEW " '|| rec_.object_name || ' " COMPILE ';
else
str_sql := 'ALTER PACKAGE " '|| rec_.object_name || ' " COMPILE BODY ';
end if;
execute immediate str_sql;
---本来我想把Exception放到这个位置的,结果执行时就是出错
end loop;
EXCEPTION WHEN OTHERS THEN
--放在这不出错但不是我想要的效果,只要中间有一个异常整体
--结束了,我是想要碰到异常继续下一个循环而不是退出.
dbms_output.put_line(sqlerrm);
end;
------解决方法--------------------------------------------------------
declare
str_sql varchar2(2000);
cursor get_obj is
select object_type, object_name from USER_OBJECTS
where status = 'INVALID '
and object_type in( 'PACKAGE BODY ', 'VIEW ');
begin
for rec_ in get_obj loop
if (rec_.object_type = 'VIEW ') then
str_sql := 'ALTER VIEW " '|| rec_.object_name || ' " COMPILE ';
else
str_sql := 'ALTER PACKAGE " '|| rec_.object_name || ' " COMPILE BODY ';
end if;
begin
execute immediate str_sql;
exception
when .....
end;
---本来我想把Exception放到这个位置的,结果执行时就是出错
end loop;
EXCEPTION WHEN OTHERS THEN
--放在这不出错但不是我想要的效果,只要中间有一个异常整体
--结束了,我是想要碰到异常继续下一个循环而不是退出.
dbms_output.put_line(sqlerrm);
end