You are on page 1of 14

PL/SQL Cursors

Cursors
n

Cursors allow embedded SQL statements n Result is a set (table) in a temporary work area Cursor name permits iterative manipulation of rows Two varieties of cursors n Implicit n Explicit
More detailed to write, Permit more advanced manipulations

n n

Example
A SIMPLE CURSOR

A SIMPLE CURSOR
Declare cursor c1 is select * from emp; BEGIN for emp_rec in c1 loop dbms_output.put_line(emp_rec.ename); end loop; END;

SQL> create or replace procedure sumsalary IS cursor c1 is select * from emp; salsum integer; BEGIN salsum := 0; for emp_rec in c1 loop salsum := salsum + emp_rec.sal; end loop; dbms_output.put_line('Salary sum: ' || salsum); END; Procedure created.
SQL> execute sumsalary; Salary sum: 29025 PL/SQL procedure successfully completed.

Declaration

Explicit Cursor Operations


n

DECLARE n Introduces cursor object, bound to SQL statement OPEN n Constructs temporary table FETCH n Accesses next row TESTS n %FOUND, %NOTFOUND, %ROWCOUNT, %ISOPEN CLOSE n Releases temporary table and associated working data

DECLARE

Introduces cursor object, bound to SQL statement


CURSOR EMP_CUR IS select empno, ename, sal from emp;

OPEN

Constructs temporary table


BEGIN open emp_cur;

FETCH accesses next row


LOOP fetch emp_cur into v_empno, v_ename, v_sal;

TESTS
%FOUND, %NOTFOUND, %ROWCOUNT, %ISOPEN
LOOP fetch emp_cur into v_empno, v_ename, v_sal; EXIT when emp_cur%NOTFOUND; IF (v_sal > 1000) then
DBMS_OUTPUT.put_line(v_empno || ' ' || v_ename || ' ' || v_sal);

ELSE DBMS_OUTPUT.put_line(v_ename || ' sal is less then 1000'); END IF; END LOOP;

CLOSE
Releases temporary table and associated working data
close emp_cur;

Writing complete Explicit Cursor


A complete example
Just Remember

DECLARE

OPEN

FETCH

TESTS

CLOSE

Example Problem
We will retrieve all records from EMP table and display them one by one by using CURSOR. With some data manipulation.

CREATE OR REPLACE PROCEDURE MY_TEST IS v_empno emp.empno%type; v_ename emp.ename%type; DECLARE v_sal emp.sal%type; CURSOR EMP_CUR IS select empno, ename, sal from emp; BEGIN open emp_cur; OPEN LOOP fetch emp_cur into v_empno, v_ename, v_sal; FETCH EXIT when emp_cur%NOTFOUND; TESTS IF (v_sal > 1000) then DBMS_OUTPUT.put_line(v_empno || ' ' || v_ename || ' ' || v_sal); ELSE DBMS_OUTPUT.put_line(v_ename || ' sal is less then 1000'); END IF; END LOOP; close emp_cur; CLOSE DBMS_OUTPUT.put_line('Execution Complete'); END;

Example-2

DECLARE a emp.empno%TYPE; b emp.ename%TYPE; CURSOR T1Cursor IS /* Cursor declaration: */

SELECT empno, ename FROM emp; BEGIN OPEN T1Cursor; LOOP /* Retrieve each row of the result of the above query into variables: */ FETCH T1Cursor INTO a, b; /* If there are no more rows to fetch, exit the loop: */ EXIT WHEN T1Cursor%NOTFOUND; DBMS_output.put_line(a || ' , ' || b); END LOOP; /* Free cursor used by the query. */ CLOSE T1Cursor; END;

Example-3
Using %ROWTYPE

Declare CURSOR EMP_CUR IS Defining ROWTYPE variable select * from emp; var_cur EMP%ROWTYPE; BEGIN open emp_cur; FETCH LOOP fetch emp_cur into var_cur; EXIT when emp_cur%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(var_cur.empno || ' ' || var_cur.ename ||' ' || var_cur.job || ' ' ||var_cur.mgr || ' ' || var_cur.hiredate|| ' ' ||var_cur.sal || ' ' || var_cur.deptno);

END LOOP; close emp_cur;


DBMS_OUTPUT.put_line('Execution Complete');

END;
/

Example 4
Fetching Limited rows

Declare CURSOR EMP_CUR IS ws o r select * from emp; ited m var_cur EMP%ROWTYPE; i l ng i BEGIN h c Fet open emp_cur; LOOP fetch emp_cur into var_cur; EXIT when emp_cur%ROWCOUNT = 5;
DBMS_OUTPUT.PUT_LINE(var_cur.empno || ' ' || var_cur.ename ||' ' || var_cur.job || ' ' ||var_cur.mgr || ' ' || var_cur.hiredate|| ' ' ||var_cur.sal || ' ' || var_cur.deptno);

END LOOP; close emp_cur;


DBMS_OUTPUT.put_line('Execution Complete');

END;
/

10

Practice

Write a cursor that will display last five row from emp table and also all rows that contain salary between 1000 to 2000.

Creating Functions

11

Function Syntax
CREATE [OR REPLACE] FUNCTION <FUNCTION NAME> (argument 1 argument 2 RETURN Number IS PLSQL BLOCK; [MODE 1] [MODE 2] datatype-1, datatype-2)

A Simple Function
CREATE OR REPLACE FUNCTION TEST (v_id in number) return number IS a number; BEGIN a := v_id +100; return a; end test;

Execute a function
Here :Z is a SQL variable SQL> variable z number; SQL> execute :z := test(10); PL/SQL procedure successfully completed. SQL> print z; Z --------110

12

Creating Function
2nd Example

CREATE OR REPLACE FUNCTION GET_SAL (v_id in emp.empno%type) return number IS v_salary emp.sal%type := 0 ; BEGIN select sal into v_salary from emp where empno = v_id; return (v_salary); end get_sal;
SQL> variable a number; SQL> Execute :a := get_sal(7934); SQL> Print a; SQL> print a; A --------1300

13

Thanks
Next:- PLSQL Packages

14

You might also like