i came across a problem on PLSQL and hope i can get here some help .
i have 2 tables – the first is called test1, and has 2 columns – customer_id and msisdn and has 2 records in it.
the other one is called test_hist and has 3 columns – customer_id, msisdn and seqno and have 1 record.
“seqno” is a sequnce number. every record that’s inseted get a seqno number which is higher by 1.
the script i wrote is :
DECLARE
CURSOR test IS
SELECT * FROM test1;
CUSTOMER_ID NUMBER;
MSISDN NUMBER;
V_SEQNO NUMBER;
BEGIN
FOR a IN test LOOP
SELECT MAX(SEQNO)+1 INTO V_SEQNO FROM test1_hist;
INSERT INTO test1_hist
select a.CUSTOMER_ID ,v_seqno, a.msisdn
from test1;
END LOOP;
commit;
END;
the problem is, that after running it, this is what i get on test_hist table :
CUSTOMER_ID SEQNO MSISDN
1.55443322 3 5422112
1.55443322 3 5422112
1.12232323 2 5454554
1.12232323 2 5454554
1.22211444 1 544643330
it runs the same times as the number of records.
if i had 4 records on test1 table, it would insert to test_hist 16 (4 of each)in total.
the original table im working with has 400k records, so i cant just do few simple insert statements…. (:
im using TOAD , the environement is ORACLE 8.
any idea what am i missing ?
thanks very much
Assaf.
you could use sequence ..then there is no need for pl/sql block to insert the data..can be done in simple insert…other wise use pl/sql with dual or values
DECLARE
CURSOR test IS
SELECT * FROM test1;