get next value of cursor if the column not found? - oracle

I am looping through cursor using record, but when this loop is unable to find a value,
it stops executing the script. So is there anyway to let the loop continue if record is empty ?
declare
cust_id info.CUSTOMER_ID%type;
SPNum info.spnumber%type;
EnterTicketDate EnteryDate%type;
cursor mycursor is
select S_P_NUM ,_START_TIME
from info
where UPPER (STATUS)='YES';
begin
for myRecord in mycursor
loop
dbms_output.put_line(SPNum);
select PID into cust_id from info where dn_num=SPNum;
dbms_output.put_line('ID is '|| cust_id);
end loop;
end;
I am thinking about avoiding this case by handing the data_not_found and move to the next value of the loop but how and is it possible ?
can I use the nextval keyword anyway?
declare
cust_id info.CUSTOMER_ID%type;
SPNum info.spnumber%type;
EnterTicketDate EnteryDate%type;
cursor mycursor is
select S_P_NUM ,_START_TIME
from info
where UPPER (STATUS)='YES';
begin
for myRecord in mycursor
loop
dbms_output.put_line(SPNum);
select PID into cust_id from info where dn_num=SPNum;
dbms_output.put_line('ID is '|| cust_id);
exit when myRecord%notfound;
end loop;
exception
when data_not_found
-- here is a recall for the next value of my record
end;

When CURSOR returns NO_DATA_FOUND it means no more data to read from it.
for myRecord in mycursor
loop
-- exit when myRecord%notfound;
/* NOT NEEDED as FOR LOOP itself will terminate on NO DATA in mycursor */
dbms_output.put_line(SPNum);
select PID into cust_id from info where dn_num=SPNum;
dbms_output.put_line('ID is '|| cust_id);
end loop;
Hope you need this.
declare
cust_id info.CUSTOMER_ID%type;
SPNum info.spnumber%type;
EnterTicketDate EnteryDate%type;
cursor mycursor is
select S_P_NUM ,_START_TIME
from info
where UPPER (STATUS)='YES';
begin
for myRecord in mycursor
loop
IF(myRecord.S_P_NUM IS NOT NULL)
THEN
BEGIN
dbms_output.put_line(myRecord.S_P_NUM);
select PID into cust_id from info where dn_num=myRecord.S_P_NUM;
dbms_output.put_line('ID is '|| cust_id);
EXCEPTION WHEN NO_DATA_FOUND THEN
dbms_output.put_line('NOT FOUND FOR SPNum '|| myRecord.S_P_NUM);
CONTINUE; -- Process Next S_P_NUM from cursor
END;
END IF;
end loop;
END;

Related

PL/SQL: How to insert depending on column value

I'm a novice at PL/SQL. I have attempted various approaches to use a Cursor to insert into a temp table depending on whether or not the value already exists in the temp table. I either get too many rows or nothing is inserted.
This is my last pseudocode approach and is the bare essence of what I'm attempt to accomplish:
DB: Oracle 12
Using SQL Developer
Goal: Take duplicate accountno info from table1 and merge / combine into single row in temptable
1. Add initial accountno info if it doesn’t already exists in temptable
2. If accountno exists in temptable add the additional info to accountno row
Suggestions are greatly appreciated.
Pseudocode
Declare
V_cnt number (20);
CURSOR c1 is select * from table1;
C1d c1%rowtype;
BEGIN
--
OPEN C1;
LOOP
FETCH C1 INTO c1d;
EXIT WHEN C1%NOTFOUND;
-- Limit attempts
IF LINE > 5 THEN EXIT; END IF;
select accountno INTO v_cnt from table1 where Exists(select 1 from temptable where accountno <> c1d.accountno);
IF v_cnt is NULL THEN
INSERT INTO temptable (accountno)
values(c1d.accountno);
END IF;
LINE:= LINE + 1;
END LOOP;
CLOSE C1;
EXCEPTION
WHEN NO_DATA_FOUND THEN
dbms_output.put_line ('NO DATA');
END;
If you strictly want to correct your pseudo code, You may try -
Declare
V_cnt number (20);
CURSOR c1 is select * from table1;
C1d c1%rowtype;
BEGIN
--
OPEN C1;
LOOP
FETCH C1 INTO c1d;
EXIT WHEN C1%NOTFOUND;
-- Limit attempts
IF LINE > 5 THEN
EXIT;
END IF;
BEGIN
SELECT accountno
INTO V_cnt
FROM temptable
WHERE accountno = c1d.accountno
AND ROWNUM = 1;
EXCEPTION
WHEN NO_DATA_FOUND THEN
V_cnt := NULL;
END;
IF V_cnt is NULL THEN
INSERT INTO temptable (accountno)
values(c1d.accountno);
END IF;
LINE:= LINE + 1;
END LOOP;
CLOSE C1;
EXCEPTION
WHEN NO_DATA_FOUND THEN
dbms_output.put_line ('NO DATA');
END;
I would strongly recommend to use below pseudo code -
Declare
V_cnt number (20);
CURSOR c1 is select * from table1;
C1d c1%rowtype;
BEGIN
--
OPEN C1;
LOOP
FETCH C1 INTO c1d;
EXIT WHEN C1%NOTFOUND;
-- Limit attempts
IF LINE > 5 THEN
EXIT;
END IF;
MERGE INTO temptable
USING table1
ON (accountno = c1d.accountno)
WHEN NOT MATCHED THEN
INSERT (accountno)
values(c1d.accountno);
LINE:= LINE + 1;
END LOOP;
CLOSE C1;
EXCEPTION
WHEN NO_DATA_FOUND THEN
dbms_output.put_line ('NO DATA');
END;

Show custom message when no rows selected in procedure [duplicate]

I have a following stored procedure in which I have used a cursor. Depending on whether the cursor return any records or not I need to do some processing.
But I am not sure how to check if the cursor return any records.
CREATE OR REPLACE PROCEDURE SP_EMPLOYEE_LOOKUP_BY_EMP_ID
(
IN_USER_ID IN NUMBER,
IN_EMPLOYEE_ID NUMBER,
IN_HC_AS_ON_DATE VARCHAR2,
emp_cursor OUT SYS_REFCURSOR
)
IS
CURSOR employees IS
SELECT * FROM EMPLOYEE e;
BEGIN
if(record exist ) then
FOR employee IN employees
LOOP
// do something
END LOOP;
else if employees is empty then
// do something else
END;
It's not possible to check if the cursor returns records without opening it.
(see here)
So you can either have some fast query just to see if there are records (using count for example),
Or, you can do it like this:
CREATE OR REPLACE PROCEDURE SP_EMPLOYEE_LOOKUP_BY_EMP_ID
(
IN_USER_ID IN NUMBER,
IN_EMPLOYEE_ID NUMBER,
IN_HC_AS_ON_DATE VARCHAR2,
emp_cursor OUT SYS_REFCURSOR
)
IS
is_found_rec boolean := false;
CURSOR employees IS
SELECT * FROM EMPLOYEE e;
BEGIN
FOR employee IN employees
LOOP
is_found_rec := true;
// do something
END LOOP;
if not is_found_rec then
// do something else
end if;
END;
I think that it possible only with FETCH. Try to use
if myCursor%found then
// some body
end if;
But if somebody know another way so correct me.
I like this way:
DECLARE
CURSOR my_cur
IS
SELECT 'a' AS a FROM DUAL WHERE 1=1;
my_rec my_cur%rowtype;
BEGIN
OPEN my_cur;
LOOP
FETCH my_cur INTO my_rec;
IF my_cur%NOTFOUND
THEN
IF my_cur%ROWCOUNT=0
THEN
-- do stuff when cursor empty
DBMS_OUTPUT.PUT_LINE('NOTFOUND,RC=0' || '_' || my_cur%ROWCOUNT || '_' || my_rec.a);
END IF;
EXIT;
ELSE
-- do stuff when cursor not empty
DBMS_OUTPUT.PUT_LINE('FOUND,RC>0' || '_' || my_cur%ROWCOUNT || '_' || my_rec.a);
END IF;
END LOOP;
CLOSE MY_CUR;
END;
Output when cursor is 1=1 (not empty):
FOUND,RC>0_1_a
Output when cursor is 1=0 (empty):
NOTFOUND,RC=0_0_
I like to use the same select query that comprises the cursor and select the count it returns into a variable. You can then evaluate the variable to determine what to do next. For example you have a cursor like this:
CURSOR c_example IS
SELECT field1
,field2
,field3
FROM table1 a
WHERE a.field1 LIKE '%something%';
Before you open the cursor to do any processing, select cursor result count into a variable.
SELECT count(*)
INTO v_exampleCount
FROM table1 a
WHERE a.field1 LIKE '%something%';
Now, again before you open cursor, do something like:
IF exampleCount>0 THEN
FOR example IN c_example
LOOP
....
END LOOP; --end example loop
....
END IF; --end example if
341/5000
One solution would be to work with reverse logic. It is e.g. not possible to ask:
IF my_record IS NULL THEN
do something;
END IF;
or
IF my_record.my_attibute IS NULL THEN
do something;
END IF;
Instead it is possible to ask:
IF my_record.my_attibute IS NOT NULL THEN
go on processing;
ELSE
do something;
END IF;

Oracle PLSQL Error while executing an anonymous block - Encountered the symbol "LOOP" [duplicate]

Please, explain me how to use cursor for loop in oracle.
If I use next code, all is fine.
for rec in (select id, name from students) loop
-- do anything
end loop;
But if I define variable for this sql statement, it doesn't work.
v_sql := 'select id, name from students';
for rec in v_sql loop
-- do anything
end loop;
Error: PLS-00103
To address issues associated with the second approach in your question you need to use
cursor variable and explicit way of opening a cursor and fetching data. It is not
allowed to use cursor variables in the FOR loop:
declare
l_sql varchar2(123); -- variable that contains a query
l_c sys_refcursor; -- cursor variable(weak cursor).
l_res your_table%rowtype; -- variable containing fetching data
begin
l_sql := 'select * from your_table';
-- Open the cursor and fetching data explicitly
-- in the LOOP.
open l_c for l_sql;
loop
fetch l_c into l_res;
exit when l_c%notfound; -- Exit the loop if there is nothing to fetch.
-- process fetched data
end loop;
close l_c; -- close the cursor
end;
Find out more
try this :
cursor v_sql is
select id, name from students;
for rec in v_sql
loop
-- do anything
end loop;
then no need to open, fetch or close the cursor.
You're not executing that sql string anywhere. Simply do this
v_sql := 'select id, name from students';
open cur for v_sql;
for rec in cur loop
-- do anything
end loop;
Or you can do this
cursor cur is select id, name from students;
open cur;
for rec in cur loop
-- do anything
end loop;
Or you can do this
for rec in (select id, name from students) loop
-- do anything
end loop
You have to use Refcursor if you are making the query at runtime. Actually refcursors are pointers to the query they wont take up any space for the rows fetched.
Normal Cursors will not work for it.
declare
v_sql varchar2(200);
rec sys_refcursor;
BEGIN
v_sql := 'select id, name from students';
open rec for v_sql
loop
fetch
exit when....
-- do anything
end loop;

how to make a loop for all columns of a table?

I have a table EMPLOYEES, i need to make a loop, on every step of the loop I use dbms_output.put_line(column_name)
How can I make this?
Just found this solution
begin
for rec in (SELECT column_name
FROM all_tab_columns
WHERE owner = 'HR'
AND table_name = 'EMPLOYEES')
loop
dbms_output.put_line(rec.column_name);
end loop;
end;
Try to use cursor as below:
set serveroutput on;
declare
var_col1 varchar2(30) ;
num_col2 number ;
CURSOR cur
is
select var_col ,num_col
from table
where <conditions>;
begin
open cur;
fetch cur into var_col1,num_col2;
LOOP
fetch cur into var_col1,num_col2;
EXIT WHEN cur%NOTFOUND;
dbms_output.put_line( var_col1||' '||num_col2);
END LOOP;
close cur;
end;
here is another solution:
set serveroutput on;
begin
FOR cur in ( select var_col ,num_col from table where <conditions>)
LOOP
dbms_output.put_line(cur.var_col||' = '||cur.num_col);
END LOOP;
end;
Of course You can rewrite this SELECT example with WHERE after table_name. I dont compiled this code but I thinks its OK. Change ID_Employee and Name for your atributes
CREATE OR REPLACE PROCEDURE AAA_TEST
IS
BEGIN
DECLARE
v_id NUMBER :=0;
v_name VARCHAR2(25);
CURSOR cursorMails IS
SELECT Id_Employee,Name FROM EMPLOYEES;
BEGIN
OPEN cursorMails;
LOOP
FETCH cursorMails INTO v_id, v_name;
DBMS_OUTPUT.PUT_LINE('Number of Employee: ' || v_id || ' Name Employee: ' || v_name);
EXIT WHEN cursorMails%NOTFOUND;
END LOOP;
CLOSE cursorMails;
END;
END;
/

How to check if cursor returns any records in oracle?

I have a following stored procedure in which I have used a cursor. Depending on whether the cursor return any records or not I need to do some processing.
But I am not sure how to check if the cursor return any records.
CREATE OR REPLACE PROCEDURE SP_EMPLOYEE_LOOKUP_BY_EMP_ID
(
IN_USER_ID IN NUMBER,
IN_EMPLOYEE_ID NUMBER,
IN_HC_AS_ON_DATE VARCHAR2,
emp_cursor OUT SYS_REFCURSOR
)
IS
CURSOR employees IS
SELECT * FROM EMPLOYEE e;
BEGIN
if(record exist ) then
FOR employee IN employees
LOOP
// do something
END LOOP;
else if employees is empty then
// do something else
END;
It's not possible to check if the cursor returns records without opening it.
(see here)
So you can either have some fast query just to see if there are records (using count for example),
Or, you can do it like this:
CREATE OR REPLACE PROCEDURE SP_EMPLOYEE_LOOKUP_BY_EMP_ID
(
IN_USER_ID IN NUMBER,
IN_EMPLOYEE_ID NUMBER,
IN_HC_AS_ON_DATE VARCHAR2,
emp_cursor OUT SYS_REFCURSOR
)
IS
is_found_rec boolean := false;
CURSOR employees IS
SELECT * FROM EMPLOYEE e;
BEGIN
FOR employee IN employees
LOOP
is_found_rec := true;
// do something
END LOOP;
if not is_found_rec then
// do something else
end if;
END;
I think that it possible only with FETCH. Try to use
if myCursor%found then
// some body
end if;
But if somebody know another way so correct me.
I like this way:
DECLARE
CURSOR my_cur
IS
SELECT 'a' AS a FROM DUAL WHERE 1=1;
my_rec my_cur%rowtype;
BEGIN
OPEN my_cur;
LOOP
FETCH my_cur INTO my_rec;
IF my_cur%NOTFOUND
THEN
IF my_cur%ROWCOUNT=0
THEN
-- do stuff when cursor empty
DBMS_OUTPUT.PUT_LINE('NOTFOUND,RC=0' || '_' || my_cur%ROWCOUNT || '_' || my_rec.a);
END IF;
EXIT;
ELSE
-- do stuff when cursor not empty
DBMS_OUTPUT.PUT_LINE('FOUND,RC>0' || '_' || my_cur%ROWCOUNT || '_' || my_rec.a);
END IF;
END LOOP;
CLOSE MY_CUR;
END;
Output when cursor is 1=1 (not empty):
FOUND,RC>0_1_a
Output when cursor is 1=0 (empty):
NOTFOUND,RC=0_0_
I like to use the same select query that comprises the cursor and select the count it returns into a variable. You can then evaluate the variable to determine what to do next. For example you have a cursor like this:
CURSOR c_example IS
SELECT field1
,field2
,field3
FROM table1 a
WHERE a.field1 LIKE '%something%';
Before you open the cursor to do any processing, select cursor result count into a variable.
SELECT count(*)
INTO v_exampleCount
FROM table1 a
WHERE a.field1 LIKE '%something%';
Now, again before you open cursor, do something like:
IF exampleCount>0 THEN
FOR example IN c_example
LOOP
....
END LOOP; --end example loop
....
END IF; --end example if
341/5000
One solution would be to work with reverse logic. It is e.g. not possible to ask:
IF my_record IS NULL THEN
do something;
END IF;
or
IF my_record.my_attibute IS NULL THEN
do something;
END IF;
Instead it is possible to ask:
IF my_record.my_attibute IS NOT NULL THEN
go on processing;
ELSE
do something;
END IF;

Resources