我是PL / SQL的新手,并且遇到以下情况。
在下面的脚本中,如果他们发现任何异常,我希望脚本继续进行迭代。处理了异常,但是我无法在循环外执行continue语句。
declare
l_max_ID number;
l_Temp_val number;
type array_t is varray(135) of varchar2(30);
arrayTable array_t := array_t('YSXQTAAA', 'YSXQTAFA', ... );
array array_t := array_t('YSXQNAAA', 'YSXQNAFA', ...);
begin
dbms_output.put_line(arrayTable.count);
for i in 1..arrayTable.count loop
dbms_output.put_line('Tabla impactada ' || arrayTable(i));
execute immediate 'select max(id)+1 from ' || arrayTable(i) into l_max_ID;
execute immediate 'alter sequence ' || array(i) || ' restart start with ' || TO_CHAR(l_Max_ID);
dbms_output.put_line('alter sequence RS1.' || array(i) || ' restart start with ' || TO_CHAR(l_Max_ID));
end loop;
rollback;
EXCEPTION
WHEN NO_DATA_FOUND THEN
/* HAndle an error that gets raised when a query returns nothing */
dbms_output.put_line('Error 1');
WHEN TOO_MANY_ROWS THEN
/* HAndle the situation when too much data is returned such as with a select-into */
dbms_output.put_line('Error 2');
WHEN OTHERS THEN
dbms_output.put_line('Error 3');
end;
问题在于某些表没有id列,而select抛出异常。该数组包含一百多个元素,删除每个元素会花费很多时间。
答案 0 :(得分:5)
将异常处理块放入循环内:
begin
dbms_output.put_line(arrayTable.count);
for i in 1..arrayTable.count loop
begin
dbms_output.put_line('Tabla impactada ' || arrayTable(i));
execute immediate 'select max(id)+1 from ' || arrayTable(i) into l_max_ID;
execute immediate 'alter sequence ' || array(i) || ' restart start with ' || TO_CHAR(l_Max_ID);
dbms_output.put_line('alter sequence RS1.' || array(i) || ' restart start with ' || TO_CHAR(l_Max_ID));
EXCEPTION
WHEN NO_DATA_FOUND THEN
/* HAndle an error that gets raised when a query returns nothing */
dbms_output.put_line('Error 1');
continue;
WHEN TOO_MANY_ROWS THEN
/* HAndle the situation when too much data is returned such as with a select-into */
dbms_output.put_line('Error 2');
continue;
WHEN OTHERS THEN
dbms_output.put_line('Error 3');
continue;
end;
end loop;
rollback;
end;