Professional Documents
Culture Documents
Cursors
Cursors
Copyright © 2006, Oracle. All rights reserved. 7-3 Copyright © 2006, Oracle. All rights reserved.
7-2 Copyright © 2006, Oracle. All rights reserved. 7-4 Copyright © 2006, Oracle. All rights reserved.
1
Controlling Explicit Cursors Declaring the Cursor
Syntax:
No
CURSOR cursor_name IS
select_statement;
Yes
DECLARE OPEN FETCH EMPTY? CLOSE
Examples
DECLARE
• Create a • Identify the • Load the • Test for • Release the CURSOR emp_cursor IS
named active set. current existing active set. SELECT employee_id, last_name FROM employees
SQL area. row into rows. WHERE department_id =30;
variables.
• Return to DECLARE
FETCH if locid NUMBER:= 1700;
rows are CURSOR dept_cursor IS
found. SELECT * FROM departments
WHERE location_id = locid;
...
7-5 Copyright © 2006, Oracle. All rights reserved. 7-7 Copyright © 2006, Oracle. All rights reserved.
2 Fetch a row.
Cursor
pointer
Cursor
3 Close the cursor. pointer
7-6 Copyright © 2006, Oracle. All rights reserved. 7-8 Copyright © 2006, Oracle. All rights reserved.
2
Opening the Cursor
DECLARE
CURSOR emp_cursor IS
SELECT employee_id, last_name FROM employees
WHERE department_id =30;
...
BEGIN
OPEN emp_cursor;
7-9 Copyright © 2006, Oracle. All rights reserved. 7-11 Copyright © 2006, Oracle. All rights reserved.
Fetching Data from the Cursor Fetching Data from the Cursor
SET SERVEROUTPUT ON
DECLARE
CURSOR emp_cursor IS
SET SERVEROUTPUT ON
SELECT employee_id, last_name FROM employees
DECLARE
WHERE department_id =30;
CURSOR emp_cursor IS
empno employees.employee_id%TYPE;
SELECT employee_id, last_name FROM employees
lname employees.last_name%TYPE;
WHERE department_id =30;
BEGIN
empno employees.employee_id%TYPE;
OPEN emp_cursor;
lname employees.last_name%TYPE;
LOOP
BEGIN
FETCH emp_cursor INTO empno, lname;
OPEN emp_cursor;
EXIT WHEN emp_cursor%NOTFOUND;
FETCH emp_cursor INTO empno, lname;
DBMS_OUTPUT.PUT_LINE( empno ||' '||lname);
DBMS_OUTPUT.PUT_LINE( empno ||' '||lname);
END LOOP;
...
...
END;
END;
/
/
7-10 Copyright © 2006, Oracle. All rights reserved. 7-12 Copyright © 2006, Oracle. All rights reserved.
3
Closing the Cursor Cursor FOR Loops
Syntax:
... FOR record_name IN cursor_name LOOP
LOOP
statement1;
FETCH emp_cursor INTO empno, lname;
EXIT WHEN emp_cursor%NOTFOUND; statement2;
DBMS_OUTPUT.PUT_LINE( empno ||' '||lname); . . .
END LOOP; END LOOP;
CLOSE emp_cursor;
END;
• The cursor FOR loop is a shortcut to process
/
explicit cursors.
• Implicit open, fetch, exit, and close occur.
• The record is implicitly declared.
7-13 Copyright © 2006, Oracle. All rights reserved. 7-15 Copyright © 2006, Oracle. All rights reserved.
7-14 Copyright © 2006, Oracle. All rights reserved. 7-16 Copyright © 2006, Oracle. All rights reserved.
4
Explicit Cursor Attributes %ROWCOUNT and %NOTFOUND: Example
SET SERVEROUTPUT ON
DECLARE
Obtain status information about a cursor. empno employees.employee_id%TYPE;
Attribute Type Description ename employees.last_name%TYPE;
CURSOR emp_cursor IS SELECT employee_id,
%ISOPEN Boolean Evaluates to TRUE if the cursor is last_name FROM employees;
open BEGIN
OPEN emp_cursor;
%NOTFOUND Boolean Evaluates to TRUE if the most recent LOOP
fetch does not return a row FETCH emp_cursor INTO empno, ename;
EXIT WHEN emp_cursor%ROWCOUNT > 10 OR
%FOUND Boolean Evaluates to TRUE if the most recent emp_cursor%NOTFOUND;
fetch returns a row; complement of DBMS_OUTPUT.PUT_LINE(TO_CHAR(empno)
%NOTFOUND ||' '|| ename);
%ROWCOUNT Number Evaluates to the total number of END LOOP;
rows returned so far CLOSE emp_cursor;
END ;
/
7-17 Copyright © 2006, Oracle. All rights reserved. 7-19 Copyright © 2006, Oracle. All rights reserved.
• Fetch rows only when the cursor is open. There is no need to declare the cursor.
• Use the %ISOPEN cursor attribute before
Example
performing a fetch to test whether the cursor is
open. SET SERVEROUTPUT ON
BEGIN
Example FOR emp_record IN (SELECT employee_id, last_name
FROM employees WHERE department_id =30)
IF NOT emp_cursor%ISOPEN THEN LOOP
OPEN emp_cursor; DBMS_OUTPUT.PUT_LINE( emp_record.employee_id ||'
'||emp_record.last_name);
END IF;
END LOOP;
LOOP
END;
FETCH emp_cursor... /
7-18 Copyright © 2006, Oracle. All rights reserved. 7-20 Copyright © 2006, Oracle. All rights reserved.
5
Cursors with Parameters FOR UPDATE Clause
Syntax: Syntax:
CURSOR cursor_name SELECT ...
[(parameter_name datatype, ...)] FROM ...
IS FOR UPDATE [OF column_reference][NOWAIT | WAIT n];
select_statement;
• Use explicit locking to deny access to other
• Pass parameter values to a cursor when the sessions for the duration of a transaction.
cursor is opened and the query is executed. • Lock the rows before the update or delete.
• Open an explicit cursor several times with a
different active set each time.
OPEN cursor_name(parameter_value,.....) ;
7-21 Copyright © 2006, Oracle. All rights reserved. 7-23 Copyright © 2006, Oracle. All rights reserved.
SET SERVEROUTPUT ON
DECLARE
CURSOR emp_cursor (deptno NUMBER) IS
SELECT employee_id, last_name
FROM employees
WHERE department_id = deptno;
dept_id NUMBER;
lname VARCHAR2(15);
BEGIN
OPEN emp_cursor (10);
...
CLOSE emp_cursor;
OPEN emp_cursor (20);
...
7-22 Copyright © 2006, Oracle. All rights reserved. 7-24 Copyright © 2006, Oracle. All rights reserved.
6
WHERE CURRENT OF Clause Summary
7-26 Copyright © 2006, Oracle. All rights reserved. 7-28 Copyright © 2006, Oracle. All rights reserved.
7
7-29 Copyright © 2006, Oracle. All rights reserved. 7-31 Copyright © 2006, Oracle. All rights reserved.
7-30 Copyright © 2006, Oracle. All rights reserved. 7-32 Copyright © 2006, Oracle. All rights reserved.
8
7-33 Copyright © 2006, Oracle. All rights reserved.