I want to insert records in table B from table A if the primary key of table A is not found in table B. So, I wrote foll. PL/SQL code.
declare
l_request_id NUMBER;
g_end_program EXCEPTION;
CURSOR
reqs
IS SELECT REQUEST_ID FROM table_A
WHERE
ACTUAL_COMPLETION_DATE <= SYSDATE
AND STATUS_CODE = 'C'
AND PHASE_CODE = 'C'
AND PRINTER != 'noprint'
AND NUMBER_OF_COPIES > 0;
BEGIN
FOR cr IN reqs
LOOP
SELECT request_id INTO l_request_id
FROM table_B
WHERE request_id = cr.request_id;
if l_request_id is NULL
then
INSERT INTO table_B from cursor....
end if;
END LOOP;
EXCEPTION
when no_data_found then
raise g_end_program;
END;
/
But, it is giving me
declare
*
ERROR at line 1:
ORA-06510: PL/SQL: unhandled user-defined exception
ORA-06512: at line 26
ORA-01403: no data found
Pl. help.
Thanks in Adv.
Sam
------------------------
To handle yourself, use your head. To handle others, use your heart
Insert Into table_B
SELECT REQUEST_ID, etc, etc
FROM table_A
WHERE
ACTUAL_COMPLETION_DATE <= SYSDATE
AND STATUS_CODE = 'C'
AND PHASE_CODE = 'C'
AND PRINTER != 'noprint'
AND NUMBER_OF_COPIES > 0
and not exists
(Select * From table_B
where table_B.request_id = table_A.request_id)
;
Originally posted by DaPi This does not do what you want (I think) - it will raise a no_data_found exception if the row doesn't exist.
My requirement is that if record is not found in table_B, then it should insert record with values from cursor.
There is a no_data_found exception handler in the procedure. Still why error occurs.
Guys, I am a beginner in development. Pl. help.
Thanks in Adv.
Sam
------------------------
To handle yourself, use your head. To handle others, use your heart
Originally posted by DaPi What's wrong with something like:
Insert Into table_B
SELECT REQUEST_ID, etc, etc
FROM table_A
WHERE
ACTUAL_COMPLETION_DATE <= SYSDATE
AND STATUS_CODE = 'C'
AND PHASE_CODE = 'C'
AND PRINTER != 'noprint'
AND NUMBER_OF_COPIES > 0
and not exists
(Select * From table_B
where table_B.request_id = table_A.request_id)
;
The first time you find a row you would insert, you get a no_data_found, which jumps out of the loop, raises your g_end_program exception, which is not handled, so it dies.
Try something like:
Code:
. . .
BEGIN
FOR cr IN reqs
LOOP
BEGIN
SELECT request_id INTO l_request_id
FROM table_B
WHERE request_id = cr.request_id;
EXCEPTION
when no_data_found then
INSERT INTO table_B from cursor....
END;
END LOOP;
COMMIT; ? ? ?
END;
/
Originally posted by DaPi The first time you find a row you would insert, you get a no_data_found, which jumps out of the loop, raises your g_end_program exception, which is not handled, so it dies.
Try something like:
Code:
. . .
BEGIN
FOR cr IN reqs
LOOP
BEGIN
SELECT request_id INTO l_request_id
FROM table_B
WHERE request_id = cr.request_id;
EXCEPTION
when no_data_found then
INSERT INTO table_B from cursor....
END;
END LOOP;
COMMIT; ? ? ?
END;
/
Hi,
With this logic, it gives the foll. error.
ERROR at line 62:
ORA-06550: line 62, column 7:
PLS-00201: identifier 'CR.REQUEST_ID' must be declared
ORA-06550: line 49, column 7:
PL/SQL: SQL Statement ignored
It seems that it is loosing the cursor out side the loop.
Pl. advise.
Thanks,
Last edited by samdba; 01-06-2005 at 09:33 AM.
Sam
------------------------
To handle yourself, use your head. To handle others, use your heart
Bookmarks