I have two tables, dcr_details_new and dcr_details_old where the primary key is DCRDID.
Both dcr_details_new and dcr_details_old tables share the same structure.The primary key is DCRDID where I increment by one in each insert.
I need to fetch all the rows from dcr_details_old into dcr_details_new where I have to filter the records by DOCREGNO and DCR_No
So a normal single row import/insert is running exactly as follows.
INSERT INTO dcr_details_new
SELECT (select Max(DCRDID) + 1 from dcr_details_new),
TWNCODE,
'100008',
DOCCATOGARY,
DCR_NO,
VISIT_NO,
GIVEAWAY,
COMPETITORBRN,
REMARK,
DCRDRDATE,
COM_ACTI
FROM dcr_details_old
WHERE DOCREGNO= 'T10037'
and DCR_NO = 28766;
1 rows created.
Now I want to skip filtering by dcr_no
and insert the records as it contain too many records with different dcr_no
's. Here when I filter only with DOCREGNO
the select statements returns many records and when I try to loop the and insert I always mess up with the primary key as it is not incrementing as I wish.
Here is the loop I have tried and the error I'm getting.
declare
i integer := 1;
BEGIN
FOR x IN (select * from dcr_details_old WHERE DOCREGNO= 'T10037')
LOOP
INSERT INTO dcr_details_new (DCRDID, TWNCODE, DOCREGNO, DOCCATOGARY,
DCR_NO, VISIT_NO, GIVEAWAY, COMPETITORBRN,
REMARK, DCRDRDATE, COM_ACTI)
SELECT (select Max(DCRDID) + 1 from dcr_details_new),
TWNCODE,
'100008',
DOCCATOGARY,
DCR_NO, VISIT_NO, GIVEAWAY, COMPETITORBRN,
REMARK, DCRDRDATE, COM_ACTI
FROM dcr_details_old;
i := i + 1;
END LOOP;
END;
Error:
**ERROR at line 1:
ORA-00001: unique constraint (SYS_C0061873) violated
ORA-06512: at line 5**
In this error SYS_C0061873
is the primary key DCRDID
.
Here in insert the value '100008'
is the new docregno
I have to put manually.
Any simple way to proceed? Kindly seek your assistance.
To insert all records from OLD table to the new table with new primary keys use following statement.
Note that the key part is to get the maximal existing key and increates it using ROWNUM. For a signle migration step (i.e. no parallel migration and the application is down) this is a secure way.
INSERT INTO dcr_details_new
(DCRDID,TWNCODE,DOCREGNO,DOCCATOGARY,DCR_NO,VISIT_NO,GIVEAWAY,
COMPETITORBRN,REMARK,DCRDRDATE,COM_ACTI)
select
(select max(DCRDID) from dcr_details_new) + rownum as DCRDID,
TWNCODE,DOCREGNO,DOCCATOGARY,DCR_NO,VISIT_NO,GIVEAWAY,
COMPETITORBRN,REMARK,DCRDRDATE,COM_ACTI
from dcr_details_old where DOCREGNO = 'T10037'
;