PL/SQL Cursor just check if value is greater than a value - plsql

I want
to make a procedure check if exist empno from table employees
with emno greater than 100. If exist at least one, i want
to exit from the loop.
How can I modify the following code ?
Is it problem I don;t use %NOTFOUND , %ROWCOUNT ?
CREATE OR REPLACE procedure check_value
IS
cursor c1 is
select *
from employess;
c1_values c1%ROWTYPE;
BEGIN
open c1;
fetch c1 into c1_values;
loop
if c1_values.EMPNO > 100 then
DBMS_OUTPUT.put_line ('Found row with empno > 100');
end if;
end loop;
close c1;
END;

If you just need to check if you have a record which has empno over 100 you can use EXISTS statement e.g.
DECLARE
CURSOR c1 IS
SELECT
CASE
WHEN EXISTS (
SELECT
1
FROM
employees
WHERE
empno > 100
) THEN 1
ELSE 0
END AS empno_exists
FROM
dual;
ln_empno_exists PLS_INTEGER;
BEGIN
OPEN c1;
FETCH c1 INTO ln_empno_exists;
CLOSE c1;
DBMS_OUTPUT.PUT_LINE('Empno over 100 exists: '||CASE WHEN ln_empno_exists = 1 THEN 'TRUE' ELSE 'FALSE' END);
END;
/
EDIT: If you want to fetch the rows with some conditions, you simply need to adjust your WHERE clause in your SELECT statement. Here you have an example with some ways to iterate through the records fetched:
DECLARE
CURSOR c1 IS
SELECT
emp.*
FROM
employees emp
WHERE
empno > 100
;
lr_c1_rec c1%ROWTYPE;
BEGIN
-- Using FOR loop
DBMS_OUTPUT.PUT_LINE('START: Printing employees records where empno > 100');
FOR rec IN c1
LOOP
DBMS_OUTPUT.PUT_LINE('empno = '||rec.empno);
END LOOP;
DBMS_OUTPUT.PUT_LINE('END: Printing employees records where empno > 100');
-- Using a LOOP with EXIT clause
DBMS_OUTPUT.PUT_LINE('START: Printing employees records where empno > 100');
OPEN c1;
LOOP
FETCH c1 INTO lr_c1_rec;
-- exit the loop when your cursor doesn't have any more records to be returned
EXIT WHEN c1%NOTFOUND;
DBMS_OUTPUT.PUT_LINE('empno = '||lr_c1_rec.empno);
END LOOP;
DBMS_OUTPUT.PUT_LINE('END: Printing employees records where empno > 100');
-- Using WHILE loop
DBMS_OUTPUT.PUT_LINE('START: Printing employees records where empno > 100');
OPEN c1;
FETCH c1 INTO lr_c1_rec;
-- As long as cursor returns any values, iterate through the records returned
WHILE c1%FOUND
LOOP
DBMS_OUTPUT.PUT_LINE('empno = '||lr_c1_rec.empno);
END LOOP;
DBMS_OUTPUT.PUT_LINE('END: Printing employees records where empno > 100');
END;
/

Just add an EXIT statement after you've found your value:
CREATE OR REPLACE procedure check_value IS
cursor c1 is
select *
from employess;
c1_values c1%ROWTYPE;
BEGIN
open c1;
loop
fetch c1 into c1_values;
IF c1%NOTFOUND THEN EXIT;
if c1_values.EMPNO > 100 then
DBMS_OUTPUT.put_line ('Found row with empno > 100');
EXIT;
end if;
end loop;
-- The EXIT statement will drop you out of the loop and leave you here
close c1;
END;
Note that I moved the fetch inside the loop and added a %NOTFOUND check. Without the %NOTFOUND the code would never know the cursor was out of data and would probably go into an infinite loop.

Why to define cursor and then open and fetch when it can be done by just
SQL But if it is required so then use FOR loop and EXIT condition
as mentioned
below. Hope it helps
CREATE OR REPLACE PROCEDURE check_value
IS
BEGIN
FOR I IN
(SELECT * FROM employess
)
LOOP
IF I.EMPNO > 5 THEN
DBMS_OUTPUT.put_line ('Found row with empno > 100');
EXIT;
END IF;
END LOOP;
END;

Related

oracle plsql procedure dynamic count of the tables in cursor loop

I want to update all the tables having ABC column.Need to skip the tables which doesn't have data.I am having problem in checking the count of the table in a cursor loop.
PLSQL code
create or replace procedure testp is
CURSOR c_testp
IS
SELECT table_name,
column_name
FROM all_tab_columns
WHERE column_name IN('ABC')
ORDER BY table_name;
c int;
BEGIN
FOR table_rec IN c_testp
LOOP
BEGIN
SELECT COUNT(*)
INTO c
FROM table_rec.table_name;
IF(c>0) THEN
query := 'update '||table_rec.table_name||' set '||table_rec.column_name ||'= xyz';
EXECUTE IMMEDIATE query;
COMMIT;
END IF;
EXCEPTION
WHEN no_data_found THEN
dbms_output.put_line('data not found');
WHEN OTHERS THEN
dbms_output.put_line('others');
END;
END LOOP;
END;
In your code, use this:
EXECUTE IMMEDIATE 'SELECT count(*) FROM ' || table_rec.table_name INTO c;
instead of this:
SELECT COUNT(*)
INTO c
FROM table_rec.table_name;
However, as mentioned in comments - there is actually no need to perform that condition check, as no update will be performed when table is empty.

I create one PL/SQL procedure i want simple show two messages?

The PL/SQL procedure below:
'DECLARE
V_EMPNO NUMBER(10):=&EMPNO;
V_EMPNO2 NUMBER(10):= 0;
CURSOR C1 IS SELECT EMPNO FROM EMP;
BEGIN
FOR I IN C1 LOOP
FETCH C1 INTO V_EMPNO2;
EXIT WHEN C1%FOUND;
END LOOP;
IF (LENGTH(V_EMPNO)) > 4 THEN
DBMS_OUTPUT.PUT_LINE ('LENGTH OF EMPNO GREATER THAN 4 NUMBER');
ELSIF (V_EMPNO = V_EMPNO2) THEN
DBMS_OUTPUT.PUT_LINE ('THIS EMPLOYEE NUMBER ALREADY EXIST');
END IF;
END;
/'
In this procedure I want show two messages
one is if lenght greater than number 4 than show message
and second is if v_empno = v_empno2 then show second message
empno = v_empno then show message:
DBMS_OUTPUT.PUT_LINE ('THIS EMPLOYEE NUMBER ALREADY EXIST')
this is error
Enter value for empno: 4444
DECLARE
*
ERROR at line 1:
ORA-01001: invalid cursor
ORA-06512: at line 7
FOR I IN C1 LOOP
already implicitly opens c1 and handles the fetching, so your explicit fetch after it is invalid.
btw i is normally used for numeric indexes rather than records.
Also your caps lock was on when you wrote that code ;)
I think there are a couple of problems with your code.
C1 does not restrict on employee number (meaning the loop will
return a single, largely random row from table emp
You are mixing FOR LOOP and FETCH syntax
Variable v_empno is a NUMBER and you need to be careful when checking the length - you need to explicitly TO_CHAR and control the format - often TO_CHAR will end up including space characters (an alternative would be to check the value of a number is < 10000)
I've not tested this code but this might be closer to what you're after :
DECLARE
l_empno NUMBER := &empno ;
CURSOR C_get_emp
IS
SELECT e.empno
FROM emp e
WHERE e.empno = l_empno
;
R_emp C_get_emp%ROWTYPE ;
BEGIN
IF LENGTH(TRIM(TO_CHAR(l_empno))) > 4 THEN
DBMS_OUTPUT.put_line('Length of empno > 4') ;
ELSE
OPEN C_get_emp ;
FETCH C_get_emp INTO R_emp ;
IF C_get_emp%FOUND THEN
DBMS_OUTPUT.put_line('Employee number already exists') ;
END IF ;
CLOSE C_get_emp ;
END IF ;
END ;

Forms on procedure in apex

I want to create form on procedure in APEX
this is my code
create or replace procedure "RESERVER2"
(numo_place in number,CATGE IN varchar,NOM_EQUIP1 IN VARCHAR2,NOM_EQUIP2 IN VARCHAR)
is
V_MAX CLIENT1.MAXIMUM%TYPE;
V_MIN CLIENT1.MINIMUM%TYPE;
V_CATG CLIENT1.CATG%TYPE;
V_NOM_EQUIPE1 CLIENT.NOM_EQUIPE1%TYPE;
V_NOM_EQUIPE2 CLIENT.NOM_EQUIPE2%TYPE;
V_num reservation.num_place%type;
CURSOR C1 IS
select CATG,NOM_EQUIPE1,NOM_EQUIPE2 from client1;
begin
open C1;
LOOP
FETCH C1 into V_CATG,V_NOM_EQUIPE1,V_NOM_EQUIPE2;
EXIT WHEN C1%notfound;
select CATG into V_CATG from CLIENT1;
if CATGE=V_CATG THEN
INSERT INTO RESERVATION(CATEG)values(CATGE);
select NOM_EQUIPE1 into V_NOM_EQUIPE1 from CLIENT1;
if NOM_EQUIP1=V_NOM_EQUIPE1 THEN
INSERT INTO RESERVATION(NOM_MATCH)values(NOM_EQUIP1);
if CATGE='A'AND numo_place<20 then
select NOM_EQUIPE2 into V_NOM_EQUIPE2 from CLIENT1;
if NOM_EQUIP2=V_NOM_EQUIPE2 THEN
INSERT INTO RESERVATION(NOM_MATCH2)values(NOM_EQUIP2);
if CATGE='A'AND numo_place<20 then
INSERT INTO RESERVATION(NUM_PLACE) values(numo_place);
else
DBMS_OUTPUT.PUT_LINE('ERREUR');
end if;
end if;
end if;
end if;
end if;
end loop;
close C1;
end;
When I run my form and I start fill the text field after I click in submit
this error appears:
ORA-01422: exact fetch returns more than requested number of rows
Any advice?

Can I pass an explicit cursor to a function/procedure for use in FOR loop?

I have a procedure that performs some calculations on all records returned by a cursor. It looks a bit like this:
PROCEDURE do_calc(id table.id_column%TYPE)
IS
CURSOR c IS
SELECT col1, col2, col3
FROM table
WHERE ...;
BEGIN
FOR r IN c LOOP
-- do some complicated calculations using r.col1, r.col2, r.col3 etc.
END LOOP;
END;
Now I have the case where I need to perform the exact same calculation on a different set of records that come from a different table. However, these have the same "shape" as in the above in example.
Is it possible to write a procedure that looks like this:
PROCEDURE do_calc2(c some_cursor_type)
IS
BEGIN
FOR r IN c LOOP
-- do the calc, knowing we have r.col1, r.col2, r.col3, etc.
END LOOP;
END;
I know about SYS_REFCURSOR, but I was wondering if it was possible to use the much more convenient FOR ... LOOP syntax and implicit record type.
Create a package.
Declare your cursor as package variable.
Use %rowtype to set function parameter type.
create or replace package test is
cursor c is select 1 as one, 2 as two from dual;
procedure test1;
function test2(test_record c%ROWTYPE) return number;
end test;
create or replace package body test is
procedure test1 is
begin
for r in c loop
dbms_output.put_line(test2(r));
end loop;
end;
function test2(test_record c%ROWTYPE) return number is
l_summ number;
begin
l_summ := test_record.one + test_record.two;
return l_summ;
end;
end test;
I had a similar problem, where I had two cursors that needed to be processed the same way, so this is how I figured it out.
DECLARE
--Define our own rowType
TYPE employeeRowType IS RECORD (
f_name VARCHAR2(30),
l_name VARCHAR2(30));
--Define our ref cursor type
--If we didn't need our own rowType, we could have this: RETURN employees%ROWTYPE
TYPE empcurtyp IS REF CURSOR RETURN employeeRowType;
--Processes the cursors
PROCEDURE process_emp_cv (emp_cv IN empcurtyp) IS
person employeeRowType;
BEGIN
LOOP
FETCH emp_cv INTO person;
EXIT WHEN emp_cv%NOTFOUND;
DBMS_OUTPUT.PUT_LINE('Name = ' || person.f_name ||
' ' || person.l_name);
END LOOP;
END;
--Defines the cursors
PROCEDURE mainProcedure IS
emp empcurtyp;
BEGIN
OPEN emp FOR SELECT first_name, last_name FROM employees WHERE salary > 50000;
process_emp_cv(emp);
CLOSE emp;
OPEN emp FOR SELECT first_name, last_name FROM kuren WHERE first_name LIKE 'J%';
process_emp_cv(emp);
CLOSE emp;
END;
BEGIN
mainProcedure;
END;
/
You can also use this if you want to bulk collect your cursors. You just need to change your helper procedure process_emp_cv; the rest can stay the same.
Using BULK COLLECT
--Processes the cursors
PROCEDURE process_emp_cv (emp_cv IN empcurtyp) IS
TYPE t_employeeRowTable IS TABLE OF employeeRowType;
employeeTable t_employeeRowTable;
BEGIN
LOOP
FETCH emp_cv BULK COLLECT INTO employeeTable LIMIT 50;
FOR indx IN 1 .. employeeTable.Count
LOOP
DBMS_OUTPUT.PUT_LINE('Name = ' || employeeTable(indx).f_name ||
' ' || employeeTable(indx).l_name);
END LOOP;
EXIT WHEN emp_cv%NOTFOUND;
END LOOP;
END;
Try this one, Usong ref cursor.
declare
type c is ref cursor;
c2 c;
type rec is record(
id number,
name varchar(20)
);
r rec;
procedure p1(c1 in out c,r1 in out rec)is begin
loop
fetch c1 into r1;
exit when c1%notfound;
dbms_output.put_line(r1.id || ' ' ||r1.name);
end loop;
end;
begin
open c2 for select id, name from student;
p1(c2,r);
end;
Yes you can use Cursor explicitly into procedure and function,for that cursor need to declare into package as variable

plsql cursor iterating problem

i use oracle demo schema scott to do some plsql test ( the data in that schema are never changed ). i wrote the following program to get the employee number of each department. the problem is, there is just 4 departments but my program output 5 row. i can't find out the reason, anyone can help? great thanks.
declare
cursor employees(department_id number) is
select count(*) howmany
from scott.emp
where deptno=department_id;
employees_per_dept employees%rowtype;
cursor departments is
select *
from scott.dept;
a_department departments%rowtype;
begin
dbms_output.put_line('-----------------------------------');
open departments;
loop
exit when departments%notfound;
fetch departments into a_department;
open employees(a_department.deptno);
fetch employees into employees_per_dept;
dbms_output.put_line(employees_per_dept.howmany);
close employees;
end loop;
close departments;
dbms_output.put_line('-----------------------------------');
end;
If you output the deptno in the dbms_output you'll see the reason.
You need to switch these two lines:
fetch departments into a_department;
exit when departments%notfound;
%NOTFOUND is meaningless before the initial FETCH; your code was counting the emps in the last dept twice.
declare
cursor cl(ccode varchar2) is
Select * from employees where department_id=ccode;
z cl%rowtype;
cnt number:=0;
begin
Open cl('90');
fetch cl into Z;
while (cl%found) loop
dbms_output.put_line ( 'nsme is ' || z.last_name);
fetch cl into Z;
cnt := cnt +1;
end loop;
dbms_output.put_line (cnt);
close cl;
end;

Resources