Professional Documents
Culture Documents
PLSQL Day 7 Cursors
PLSQL Day 7 Cursors
PLSQL Day 7 Cursors
********
It is a pointer to temp memory , to perform intermediate
operations before output send to the client user.
Types of cursors: 2
1) Implicit cursor:
***************
A cursor which is used by each sql query.
This type can be assigned and managed by DB engine.
Properties:
*********
SQL%ISOPEN
It returns true if the file is opened for processing.
SQL%FOUND
It returns true if matched data found.
SQL%NOTFOUND
It returns true if there is no matched data.
SQL%ROWCOUNT
It returns an integer value which represents the
number of affected records.
2) Explicit cursors:
--------------------
Explicit cursors are useful to display multiple records from
the table using program or procedure.
Explicit cursors are defined and managed by programmer.
syntax:
CURSOR <cursor_name>
IS
SELECT . . . . . . . . . . . .
from <tbl1>,<tbl2>,....
where <cond1> and/or .
order by . . . . . . . . . . ;
syntax:
FETCH <cur_name> INTO <varname>;
Note:
At a time Fetch will take only one record from cursor.
So do the fetch operation repeatedly until all records are
fetched.
Ex: LOOP
FETCH mgrcur INTO REC;
stmt;
stmt;
EXIT WHEN (mgrcur%NOTFOUND);
stmt;
stmt;
END LOOP;
iv) Close the cursor. Then it will release memory occupied by cursor.
syntax:
CLOSE <cur_name>;
properties:-
*********
Explicit cursor property name begin with cursor name.
<cur_name>%ISOPEN
It returns true if the file is opened for processing
<cur_name>%FOUND
It returns true if there exits matched data
<cur_name>%NOTFOUND
It returns true if there is no matched data
<cur_name>%ROWCOUNT
It returns an integer value represents number of effected
records.
----------------------------------------------------------------------------
Ex:
write a procedure to find the number of records affected
if the emps are updated for the given job category with given
increment in their salary with in the table empcp?
EX:
PRICE tax
<10000 12%
>=10000 & <20000 18%
>=20000 & <40000 22%
>=40000 & <50000 25%
>=50000 28%
if (cost<10000)then
update prod_dtls
set cost=cost+(0.12*cost);
-----------------------------------------------------------------------------------
---------------------
Ex:
-->write a procedure to display employee details working
-->under given deptno?
EX:
WRITE A PROCEDURE TO DISPLAY EMP NAMES AND SALARIES
FOR GIVEN JOB OF EMPS?
Procedure created.
Ex:
=============================================
syntax:
var <cursor_name>%rowtype;
ex:
vrec c%rowtype;
Ex:
write a procedure to display the employee details along with
dept info from given deptno?
c_rec c%rowtype;
begin
open c;
dbms_output.put_line
(' Details of emps under the deptno: '||vdno);
dbms_output.put_line
(' ----------------------------------------------------');
loop
fetch c into c_rec;
exit when c%notfound;
dbms_output.put_line
(c_rec.empno||' ; '||c_rec.ename||' ; '||c_rec.sal||' ; '||
c_rec.job||' ; '||c_rec.deptno||' ; '||c_rec.dname||' ; '||
c_rec.loc);
end loop;
close c;
end proc_emp_dept_dtls;
Ex:
write a procedure to display the employee details working with
given job?
begin
open c_emp;
dbms_output.put_line(' the given job title: '||vjob);
dbms_output.put_line('all '||vjob||' information');
LOOP
FETCH c_emp INTO r;
exit when (c_emp%notfound);
dbms_output.put_line(r.empno||' '||r.ename||' '||r.job||' '||r.sal||' '||
r.hiredate||' '||r.deptno);
end loop;
close c_emp;
end proc_emp_info_with_givenjob;
sample output:
EX:
write a procedure to display employee details
( empno,ename,sal,desg,deptno) who is working
under given dept name?
CREATE OR REPLACE PROCEDURE PROC_EMP_DTLS_DNAME
(VDNAME VARCHAR2)
IS
CURSOR C_EMP
IS
SELECT EMPNO,ENAME,SAL,JOB,DEPTNO FROM EMP
WHERE DEPTNO=(SELECT DEPTNO FROM DEPT WHERE DNAME=VDNAME);
REC C_EMP%ROWTYPE;
BEGIN
OPEN C_EMP;
DBMS_OUTPUT.PUT_LINE(VDNAME ||' DEPARTMENT EMPLOYEE DETAILS ');
DBMS_OUTPUT.PUT_LINE('----------------------------------------------');
LOOP
FETCH C_EMP INTO REC;
EXIT WHEN C_EMP%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(' EMP ID: '||REC.EMPNO);
DBMS_OUTPUT.PUT_LINE(' EMP NAME: '||REC.ENAME);
DBMS_OUTPUT.PUT_LINE(' EMP SALARY: '||REC.SAL);
DBMS_OUTPUT.PUT_LINE(' EMP DESG: '||REC.JOB);
DBMS_OUTPUT.PUT_LINE(' EMP DEPTNO: '||REC.DEPTNO);
DBMS_OUTPUT.PUT_LINE(' --------------------------------');
END LOOP;
DBMS_OUTPUT.PUT_LINE
(CHR(10)||'Total number of emps: '||c_emp%rowcount);
CLOSE C_EMP;
END PROC_EMP_DTLS_DNAME;
SAMPLE OUTPUT:
Ex: write a procedure to display the employee names and salaries, their
department names and locations?
NOTE:
cursor type variable will supports all columns with in the cursor. No matter
the columns are from which table.
===================================================================================
=====================================
sample output:
-----------------------------------------------------------------------------------
-------------------------------------
Ex:
--Write a procedure to display customer name,city, actno,
balance,act_name of all customers?
create or replace procedure proc_cust_act_info
is
cursor c_acts is select cd.cname,cd.city,cad.actno,cad.act_bal,at.act_name
from cust_dtls cd, cust_act_dtls cad, act_types at
where cd.cno=cad.cust_code and cad.act_type=at.act_type;
rec c_acts%rowtype;
/* Declaring a cursor based variable , supports all columns from cursor */
begin
open c_acts;
dbms_output.put_line
(' Customers and their Accounts information ');
dbms_output.put_line
('CustName---custCity--- Account No: Account Bal Account Name');
dbms_output.put_line
('-----------------------------------------------------------------');
loop
fetch c_acts into rec;
EXIT when (c_acts%notfound);
dbms_output.put_line(rec.cname||'----'||rec.city||'---'||rec.actno||' '||
rec.Act_bal||' '||rec.act_name);
end loop;
close c_acts;
end proc_cust_act_info;
exec proc_cust_act_info;
-----------------------------------------------------------------------
Ex:
write a procedure to display product name,cost,warr and
comp name from given comp name?
Ex:
EX:
exec proc_acts_info('SB');
exec proc_acts_info('SAL');
exec proc_acts_info('DEMAT');
----------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-----------
Ex:
write a program to display emp details from given deptno?
DECLARE
cursor c(vdno number)
is
select * from emp
where deptno = vdno;
vrec emp%rowtype;
BEGIN
OPEN c(10);
loop
fetch c into vrec;
exit when c%notfound;
dbms_output.put_line
(
'empid: '||vrec.empno||' Empname: '||vrec.ename||
' emp sal: '||vrec.sal||' deptno: '||vrec.deptno
);
end loop;
close c;
end;
EX:
write a program to display employee details from given
empid using parameterized cursor?
DECLARE
cursor c(no number)
is
select * from emp
where empno = no;
tmp emp%rowtype;
BEGIN
OPEN c(7654);
FETCH C INTO TMP;
dbms_output.put_line('EMP_No: '||tmp.empno);
dbms_output.put_line('EMP_Name: '||tmp.ename);
dbms_output.put_line('EMP_Dept: '||tmp.deptno);
dbms_output.put_line('EMP_Salary:'||tmp.sal);
CLOSE c;
END;
----------------------------------------------------------------------------
REF CURSOR:
***********
Oracle 7.3 the REF CURSOR type has been available to allow
recordsets to be returned from stored procedures and functions.
Oracle 9i introduced the predefined SYS_REFCURSOR.
syn:
argname OUT SYS_REFCURSOR
Ex:
write a procedure to return to the program?
And display the dept details from program?
DECLARE
V_cursor SYS_REFCURSOR;
vdept dept%rowtype;
BEGIN
get_dept_rs( V_cursor);
DBMS_OUTPUT.PUT_LINE
(chr(10)||'Department table records: ');
LOOP
FETCH V_cursor INTO vdept;
EXIT WHEN V_cursor%NOTFOUND;
DBMS_OUTPUT.PUT_LINE
(vdept.deptno || ' | ' || vdept.dname || ' | ' || vdept.loc);
END LOOP;
CLOSE V_cursor;
END;
-----------------------------------------------------------------------------------
--
Ex:
write a procedure to display customer name,account name,account number, and account
balance of all customers?