PLSQL - Cursor Online Session Notes PDF
PLSQL - Cursor Online Session Notes PDF
• The Oracle server uses implicit cursors to parse and execute your SQL statements.
• Explicit cursors are explicitly declared by the programmer.
Implicit Cursors
• An implicit cursor is a session cursor that is constructed and
managed by PL/SQL. PL/SQL opens an implicit cursor every
time you run a SELECT or DML statement. You cannot
control an implicit cursor, but you can get information from
its attributes.
DECLARE
v_employee_id emp.empno%TYPE := 7934;
BEGIN
DELETE FROM emp WHERE empno = v_employee_id;
:rows_deleted := (SQL%ROWCOUNT || ' row deleted.');
END;
/
PRINT rows_deleted
Explicit Cursor
2. Open the cursor. The OPEN statement executes the query and
binds any variables that are referenced. Rows identified by the
query are called the active set and are now available for fetching.
3. Fetch data from the cursor. In the flow diagram shown on the
slide, after each fetch you test the cursor for any existing row. If
there are no more rows to process, then you must close the
cursor.
4. Close the cursor. The CLOSE statement releases the active set
of rows. It is now possible to reopen the cursor to establish a
fresh active set.
cursor_name is a PL/SQL identifier.
select_statement is a SELECT statement without an INTO clause.
Note: The FETCH statement
performs the following
operations:
1. Reads the data for the
current row into the output
PL/SQL variables.
2. Advances the pointer to
the next row in the identified
set.
In the syntax:
cursor_name is the name of the previously declared cursor.
variable is an output variable to store the results.
record_name is the name of the record in which the retrieved data is stored. (The record
variable can be declared using the %ROWTYPE attribute.)
EXAMPLE: Retrieve the first 10
employees one by one.
SET SERVEROUTPUT ON
DECLARE
v_empno employees.employee_id%TYPE;
v_ename employees.last_name%TYPE;
CURSOR emp_cursor IS
SELECT employee_id, last_name
FROM employees;
BEGIN
OPEN emp_cursor;
FOR i IN 1..10 LOOP
FETCH emp_cursor INTO v_empno, v_ename;
DBMS_OUTPUT.PUT_LINE (TO_CHAR(v_empno)
||’ ’|| v_ename);
END LOOP;
END ;
/
Note: The CLOSE statement releases the context area.
Use the %ROWCOUNT cursor attribute for the following:
– To retrieve an exact number of rows
– Fetch the rows in a numeric FOR loop
– Fetch the rows in a simple loop and determine when to
exit the loop.
Examples
Example-1: %NOTFOUND
30
Parameterized Cursors -Example
Given a city, this cursor returns the sales totals for
every employee from that city
31
Parameterized Cursors
• The open statement for such cursors will have
the actual augment.
open ci3(‘Wichita’);
• If a cursor for loop is used to process this cursor
for c3_rec in c3(‘Wichita’) loop
…
end loop;
32
/*displaying employees those who are working in user selected deptno using cursor with
parameter*/
set serveroutput ON;
declare
name emp.ename%TYPE;
salary emp.sal%TYPE;
dno emp.deptno%type := &enter_dept_number;
CURSOR EMP_CUR(dept_num emp.deptno%TYPE) is SELECT ename,sal from emp where
deptno=dept_num; -- cursor with parameter declaration
begin
if NOT emp_CUR%ISOPEN THEN
open emp_CUR(dno); --open the cursor with parameter
end if;
DBMS_output.put_line('employees in Department number '||dno);
LOOP
fetch emp_cur into name,salary; --fetch current record fields into local variables name and
salary
EXIT when emp_cur%NOTFOUND;
DBMS_output.put_line(name||' ' || salary);
end loop;
DBMS_output.put_line(emp_cur%rowcount|| ' employees found in under deptno '||dno);
close emp_cur; -- closing the cursor
end;
/
Update Cursor
• PL/SQL cursors can also be used to perform updates
cursor <cname> is
<select-statement> for update;
• Select statement should involve only one database
table
update <table-name>
set <set-clause>
where current of <cname>;
• to delete record from database table
delete from < table-name >
where current of <cname>;
34
Update Cursor - Example
/*using SELECT .. FOR UPDATE CURSOR and WHERE CURRENT OF,
update salary for employees in deptno 20*/
declare
CURSOR EMP_CUR is SELECT ename,sal from emp where
deptno=20 FOR UPDATE; -- cursor declaration
begin
FOR emp_rec IN emp_cur LOOP
UPDATE emp set sal=sal*1.07 where current of emp_cur;
DBMS_output.put_line(emp_rec.ename||' ' ||
emp_rec.sal);
end loop;
end;
/
35