全部博文(136)
分类: Oracle
2008-06-02 17:13:22
SQL> truncate table test_forall;
Table truncated.
SQL> desc test_forall;
Name Null? Type ------- -------- ------------- ID NUMBER(10) NAME VARCHAR2(20) SQL> declare
2 type record_test_forall is record( 3 id test_forall.id%type, 4 name varchar2(100) 5 ); 6 type taa_test_forall is table of record_test_forall index by pls_integer; 7 aa_test_forall taa_test_forall; 8 begin 9 for n_pointer in 1..10 loop 10 aa_test_forall(n_pointer).id := n_pointer; 11 aa_test_forall(n_pointer).name := 'yuechaotian' || n_pointer; 12 end loop; 13 14 -- change the 5th element, it'll be too long to be inserted. 15 aa_test_forall(5).name := 'http://yuechaotian.cublog.cn'; 16 -- before execute forall, send a insert transaction 17 insert into test_forall values(99, 'yuechaotian99'); 18 19 forall n_pointer in 1..10 20 insert into test_forall values aa_test_forall( n_pointer ); 21 22 dbms_output.put_line( 'Insert rows: ' || sql%rowcount ); 23 for n_pointer in 1..10 loop 24 dbms_output.put_line( 'Row: ' || n_pointer 25 || ' | Result: ' ||sql%bulk_rowcount(n_pointer) ); 26 end loop; 27 end; 28 / declare * ERROR at line 1: ORA-01401: inserted value too large for column ORA-06512: at line 19 SQL> select * from test_forall; no rows
selected |
SQL> rollback;
Rollback complete.
SQL> declare
2 type record_test_forall is record( 3 id test_forall.id%type, 4 name varchar2(100) 5 ); 6 type taa_test_forall is table of record_test_forall index by pls_integer; 7 aa_test_forall taa_test_forall; 8 begin 9 for n_pointer in 1..10 loop 10 aa_test_forall(n_pointer).id := n_pointer; 11 aa_test_forall(n_pointer).name := 'yuechaotian' || n_pointer; 12 end loop; 13 14 -- Change the 5th element, it'll be too long to be inserted. 15 aa_test_forall(5).name := 'http://yuechaotian.cublog.cn'; 16 -- Before execute forall, execute a insert first. 17 insert into test_forall values(99, 'yuechaotian99'); 18 19 forall n_pointer in 1..10 20 insert into test_forall values aa_test_forall( n_pointer ); 21 22 dbms_output.put_line( 'Insert rows: ' || sql%rowcount ); 23 for n_pointer in 1..10 loop 24 dbms_output.put_line( 'Row: ' || n_pointer 25 || ' | Result: ' ||sql%bulk_rowcount(n_pointer) ); 26 end loop; 27 exception 28 when others then 29 dbms_output.put_line( 'error: ' || sqlerrm ); 30 end; 31 / error: ORA-01401: inserted value too large for column PL/SQL procedure successfully completed.
SQL> select * from
test_forall;
ID NAME ---------- -------------------- 99 yuechaotian99 1 yuechaotian1 2 yuechaotian2 3 yuechaotian3 4 yuechaotian4 |
SQL> declare
2 type record_test_forall is record( 3 id test_forall.id%type, 4 name varchar2(100) 5 ); 6 type taa_test_forall is table of record_test_forall index by pls_integer; 7 aa_test_forall taa_test_forall; 8 begin 9 for n_pointer in 1..10 loop 10 aa_test_forall(n_pointer).id := n_pointer; 11 aa_test_forall(n_pointer).name := 'yuechaotian' || n_pointer; 12 end loop; 13 14 -- change the 5th element, it'll be too long to be inserted. 15 aa_test_forall(5).name := 'http://yuechaotian.cublog.cn'; 16 -- before execute forall, send a insert transaction 17 insert into test_forall values(99, 'yuechaotian99'); 18 19 forall n_pointer in 1..10 save exceptions 20 insert into test_forall values aa_test_forall( n_pointer ); 21 22 dbms_output.put_line( 'Insert rows: ' || sql%rowcount ); 23 for n_pointer in 1..10 loop 24 dbms_output.put_line( 'Row: ' || n_pointer 25 || ' | Result: ' ||sql%bulk_rowcount(n_pointer) ); 26 end loop; 27 28 exception 29 when others then 30 dbms_output.put_line( 'error: ' || sqlerrm ); 31 end; 32 / error: ORA-24381: error(s) in array DML PL/SQL procedure successfully completed.
SQL> select * from test_forall;
ID NAME
---------- -------------------- 99 yuechaotian99 1 yuechaotian1 2 yuechaotian2 3 yuechaotian3 4 yuechaotian4 6 yuechaotian6 7 yuechaotian7 8 yuechaotian8 9 yuechaotian9 10 yuechaotian10 10 rows
selected. |
SQL> rollback;
Rollback complete.
SQL> declare
2 type record_test_forall is record( 3 id test_forall.id%type, 4 name varchar2(100) 5 ); 6 type taa_test_forall is table of record_test_forall index by pls_integer; 7 aa_test_forall taa_test_forall; 8 9 bulk_errors exception; 10 pragma exception_init(bulk_errors, -24381); 11 begin 12 for n_pointer in 1..10 loop 13 aa_test_forall(n_pointer).id := n_pointer; 14 aa_test_forall(n_pointer).name := 'yuechaotian' || n_pointer; 15 end loop; 16 17 -- change the 5th element, it'll be too long to be inserted. 18 aa_test_forall(5).name := 'http://yuechaotian.cublog.cn'; 19 -- before execute forall, send a insert transaction 20 insert into test_forall values(99, 'yuechaotian99'); 21 22 begin 23 forall n_pointer in 1..10 save exceptions 24 insert into test_forall values aa_test_forall( n_pointer ); 25 exception 26 when bulk_errors then 27 for n_error_pointer in 1..sql%bulk_exceptions.count loop 28 dbms_output.put_line( 'error ' || n_error_pointer || ': The error DML number is ' 29 || sql%bulk_exceptions(n_error_pointer).error_index ); 30 dbms_output.put_line( 'The Oracle error is ' 31 || sqlerrm( -1 * sql%bulk_exceptions(n_error_pointer).error_code ) ); 32 end loop; 33 end; 34 dbms_output.put_line( 'Insert rows: ' || sql%rowcount ); 35 for n_pointer in 1..10 loop 36 dbms_output.put_line( 'Row: ' || n_pointer 37 || ' | Result: ' ||sql%bulk_rowcount(n_pointer) ); 38 end loop; 39 40 exception 41 when others then 42 dbms_output.put_line( 'error: ' || sqlerrm ); 43 end; 44 / error 1: The error DML number is 5 The Oracle error is ORA-01401: inserted value too large for column Insert rows: 9 Row: 1 | Result: 1 Row: 2 | Result: 1 Row: 3 | Result: 1 Row: 4 | Result: 1 Row: 5 | Result: 0 Row: 6 | Result: 1 Row: 7 | Result: 1 Row: 8 | Result: 1 Row: 9 | Result: 1 Row: 10 | Result: 1 PL/SQL procedure successfully completed.
SQL> select * from test_forall;
ID NAME
---------- -------------------- 99 yuechaotian99 1 yuechaotian1 2 yuechaotian2 3 yuechaotian3 4 yuechaotian4 6 yuechaotian6 7 yuechaotian7 8 yuechaotian8 9 yuechaotian9 10 yuechaotian10 10 rows
selected. |
23 exception 24 when others then 25 if sqlcode = 24381 then 26 for n_error_pointer in 1..sql%bulk_exceptions.count loop 27 dbms_output.put_line( 'error ' || n_error_pointer || ': The error DML number is ' 28 || sql%bulk_exceptions(n_error_pointer).error_index ); 29 dbms_output.put_line( 'The Oracle error is ' 30 || sqlerrm( -1 * sql%bulk_exceptions(n_error_pointer).error_code ) ); 31 end loop; 32 end if; 33 end; |