I am working on nested cursor where i have a scenario like if the cursor output does not return any data i need to insert data to a table and if it exist then i need to insert to another table say a log table.
DECLARE
CURSOR emp_details IS
SELECT
empname,
lastname
FROM
emp
WHERE
empname = 'xxx';
v_is_active VARCHAR2(50 BYTE);
v_is_include VARCHAR2(50 BYTE);
lv_emp_name emp.empname%TYPE;
lv_lastname emp.lastname%TYPE;
BEGIN
OPEN emp_details;
LOOP
FETCH emp_details INTO
lv_emp_name,
lv_lastname;
**IF emp_details%notfound** THEN
IF v_is_active = 'A' THEN
Do some activity like insert into a table T1
END IF;
IF v_is_include = 'I' THEN
Do some activity like insert into a table T2
END IF;
ELSE
INSERT INTO emp (
empname,
isnew
) VALUES (
lv_emp_name,
0
);
END IF;
-----exit : if i add here this will terminate the loop and will execute only once ie the loop will not go for next iteration. If i didnt use exit it is going on infinite loop.
-- IF IF emp_details%FOUND - IF CURSOR HAS A RCORD
END LOOP;
CLOSE emp_details;
END;
Using Notfound method i am checking if value existing and inserting the data. here my problem is i am not abl to exit the method. If i exist the method using EXIT command it just run for only one row and will not execute the cursors second row.How to exit from cursor without going on infinite loop
if, before theelse? Your logic seems strange though, normally you would justexit when c%notfound, not do any work. Are you maybe confused about what found/notfound means? Those refer to the last fetch, not whether the cursor found any rows at all. You can track that with a variable if that is what you want, but it's not clear.