exception DUP_VAL_ON_INDEX [message #291658] |
Sat, 05 January 2008 12:57  |
aligator1234
Messages: 10 Registered: January 2008
|
Junior Member |
|
|
Hallo everybody,
I have problem wirth exception DUP_VAL_ON_INDEX. I explain my problem for sample : I have following procedure:
Procedure tomek()
begin
insert into name_table
(id --id is PK
,surname)
values (ID
,SURNAME
)
select ID, SURNAME from any_table ;
exception
when DUP_VAL_ON_INDEX then
dbms_output.put_line ('value ID=' || || 'value SURNAME=' || );
end tomek;
and now my question. In exception I would like get value ID and SURNAME . Is it possible in this sample ?.
Please , help me
best regards
Tomek
|
|
|
|
|
|
Re: exception DUP_VAL_ON_INDEX [message #291664 is a reply to message #291661] |
Sat, 05 January 2008 14:56   |
 |
Littlefoot
Messages: 21825 Registered: June 2005 Location: Croatia, Europe
|
Senior Member Account Moderator |
|
|
However, as older Oracle versions don't know about logging errors as 10g does, you'll have to do it in an oldfashioned way - record by record.
In order to do that, create a loop and either log exceptions into a table (you'll have to create it), or show it on the screen. The first approach is a little bit more complicated, but - at the end - you'll benefit from it as you'll have those records stored in a table. The second one is OK if there is a small number of incorrect records (only a few) because you'll have to write that somewhere to keep a record of it.
Here is a short example:DECLARE
l_err_code VARCHAR2(512);
BEGIN
FOR cur_r IN (SELECT id, surname FROM some_table)
LOOP
BEGIN --> note this inner begin-end block! It will make possible
-- to continue the loop even though an error appears
INSERT INTO another_table (id, surname)
VALUES (cur_r.id, cur_r.surname);
EXCEPTION
WHEN OTHERS THEN
l_err_code := sqlerrm;
INSERT INTO error_log_table (id, err) VALUES (cur_r.id, l_err_code);
END;
END LOOP;
END;
|
|
|
|
|
|
|
Re: exception DUP_VAL_ON_INDEX [message #291702 is a reply to message #291692] |
Sun, 06 January 2008 02:48   |
Frank Naude
Messages: 4596 Registered: April 1998
|
Senior Member |
|
|
Will this work:
SQL> CREATE TABLE name_table(id NUMBER PRIMARY KEY, surname VARCHAR2(30));
Table created.
SQL> INSERT INTO name_table VALUES(123, 'aligator1234');
1 row created.
SQL>
SQL> CREATE OR REPLACE PROCEDURE tomek
2 AS
3 v_id name_table.id%TYPE := '123';
4 v_surname name_table.surname%TYPE := 'aligator1234';
5 BEGIN
6 INSERT INTO name_table (id, surname) VALUES (v_id, v_surname);
7
8 SELECT id, surname INTO v_id, v_surname FROM name_table;
9 EXCEPTION
10 WHEN dup_val_on_index THEN
11 dbms_output.put_line('value ID=' || v_id || 'value SURNAME=' || v_surname);
12 END tomek;
13 /
Procedure created.
SQL> exec tomek
value ID=123value SURNAME=aligator1234
PL/SQL procedure successfully completed.
|
|
|
|
|
|
|
|
|
|
Re: exception DUP_VAL_ON_INDEX [message #292894 is a reply to message #292852] |
Thu, 10 January 2008 02:29   |
aligator1234
Messages: 10 Registered: January 2008
|
Junior Member |
|
|
I must using exception DUP_VAL_ON_INDEX. Below I description my sample for clear situation.
My task is insert data into table from external_table ( table and external table is in the same database).
My procedure is following:
Remark: Primary key in Normal_table is company column.
Procedure insert_from_external_table
is
cursor c_rec is
select name, first_name from external_table;
r_rec c_rec%rowtype;
begin
open c_rec;
loop
begin
fetch c_rec into r_rec
exit when c_rec%notfound
if r_rec.name = 'M' then
insert into NORMAL_table(name, company)
values(r_rec.name, r_rec.company);
else
insert into ERROR_table (name, company)
values(r_rec.name, r_rec.company);
end if;
exception
when DUP_VAL_ON_INDEX
insert into ERROR_table
end;
end loop;
close c_rec;
end insert_from_external_table
execution time this procedure for 1 million rows is about 40 minutes. it is too much. I would like less than 10 minutes.
Any idea ?
best regards
Tomek
|
|
|
|
|
|
|
|
|