Thursday 4 January 2007

Using Bulk Collect Exceptions

Example to use Bulk Collect exceptions - new in 9i -

CREATE TABLE t (text VARCHAR2(4))

declare
type words_t is table of varchar2(10);
words words_t := words_t ( 'dog', 'fish', 'cat', 'ball', 'bat', 'spoke', 'pad','elephant' );

bulk_errors exception;
pragma exception_init ( bulk_errors, -24381 );

begin
Forall j in words.first..words.last
save exceptions
insert into t ( text ) values ( words(j) );

exception

when bulk_errors THEN
for j in 1..sql%bulk_exceptions.count
loop
Dbms_Output.Put_Line (
sql%bulk_exceptions(j).error_index || ', ' ||
Sqlerrm(-sql%bulk_exceptions(j).error_code)||words(sql%bulk_exceptions(j).error_index));
end loop;

end;

No comments: