Cursors : Introduction « Cursor « Oracle PL/SQL Tutorial






You use a cursor when you have a SELECT statement that returns more than one row from the database.

A cursor is basically a set of rows that you can access one at a time.

You retrieve the rows into the cursor using your SELECT statement and then fetch the rows from the cursor.

You may follow five steps when using a cursor:

  1. Declare variables to store the column values from the SELECT statement.
  2. Declare the cursor, specifying your SELECT statement.
  3. Open the cursor.
  4. Fetch the rows from the cursor.
  5. Close the cursor.

The syntax for declaring a cursor is as follows:

CURSOR cursor_name IS
  SELECT_statement;
where
cursor_name specifies the name of the cursor.

SELECT_statement is a SELECT statement.

You open a cursor using the OPEN statement, which must be placed in the executable section of the block.

To read each row from the cursor, you can use the FETCH statement.

The FETCH statement reads the column values into the variables that you specify;

FETCH uses the following syntax:
FETCH cursor_name
INTO variable[, variable ...];

where

  1. cursor_name specifies the name of the cursor.
  2. variable is a previously declared variable into which values from the cursor's SELECT statement are stored.

Once you've finished with the cursor, the final step is to close the cursor using the CLOSE statement.

Closing your cursors frees up system resources.

SQL>
SQL>
SQL> -- create demo table
SQL> create table Employee(
  2    ID                 VARCHAR2(4 BYTE)         NOT NULL primary key,
  3    First_Name         VARCHAR2(10 BYTE),
  4    Last_Name          VARCHAR2(10 BYTE),
  5    Start_Date         DATE,
  6    End_Date           DATE,
  7    Salary             Number(8,2),
  8    City               VARCHAR2(10 BYTE),
  9    Description        VARCHAR2(15 BYTE)
 10  )
 11  /

Table created.

SQL>
SQL> -- prepare data
SQL> insert into Employee(ID,  First_Name, Last_Name, Start_Date,                     End_Date,                       Salary,  City,       Description)
  2               values ('01','Jason',    'Martin',  to_date('19960725','YYYYMMDD'), to_date('20060725','YYYYMMDD'), 1234.56, 'Toronto',  'Programmer')
  3  /

1 row created.

SQL> insert into Employee(ID,  First_Name, Last_Name, Start_Date,                     End_Date,                       Salary,  City,       Description)
  2                values('02','Alison',   'Mathews', to_date('19760321','YYYYMMDD'), to_date('19860221','YYYYMMDD'), 6661.78, 'Vancouver','Tester')
  3  /

1 row created.

SQL> insert into Employee(ID,  First_Name, Last_Name, Start_Date,                     End_Date,                       Salary,  City,       Description)
  2                values('03','James',    'Smith',   to_date('19781212','YYYYMMDD'), to_date('19900315','YYYYMMDD'), 6544.78, 'Vancouver','Tester')
  3  /

1 row created.

SQL> insert into Employee(ID,  First_Name, Last_Name, Start_Date,                     End_Date,                       Salary,  City,       Description)
  2                values('04','Celia',    'Rice',    to_date('19821024','YYYYMMDD'), to_date('19990421','YYYYMMDD'), 2344.78, 'Vancouver','Manager')
  3  /

1 row created.

SQL> insert into Employee(ID,  First_Name, Last_Name, Start_Date,                     End_Date,                       Salary,  City,       Description)
  2                values('05','Robert',   'Black',   to_date('19840115','YYYYMMDD'), to_date('19980808','YYYYMMDD'), 2334.78, 'Vancouver','Tester')
  3  /

1 row created.

SQL> insert into Employee(ID,  First_Name, Last_Name, Start_Date,                     End_Date,                       Salary, City,        Description)
  2                values('06','Linda',    'Green',   to_date('19870730','YYYYMMDD'), to_date('19960104','YYYYMMDD'), 4322.78,'New York',  'Tester')
  3  /

1 row created.

SQL> insert into Employee(ID,  First_Name, Last_Name, Start_Date,                     End_Date,                       Salary, City,        Description)
  2                values('07','David',    'Larry',   to_date('19901231','YYYYMMDD'), to_date('19980212','YYYYMMDD'), 7897.78,'New York',  'Manager')
  3  /

1 row created.

SQL> insert into Employee(ID,  First_Name, Last_Name, Start_Date,                     End_Date,                       Salary, City,        Description)
  2                values('08','James',    'Cat',     to_date('19960917','YYYYMMDD'), to_date('20020415','YYYYMMDD'), 1232.78,'Vancouver', 'Tester')
  3  /

1 row created.

SQL>
SQL>
SQL>
SQL> -- display data in the table
SQL> select * from Employee
  2  /

ID   FIRST_NAME LAST_NAME  START_DAT END_DATE      SALARY CITY       DESCRIPTION
---- ---------- ---------- --------- --------- ---------- ---------- ---------------
01   Jason      Martin     25-JUL-96 25-JUL-06    1234.56 Toronto    Programmer
02   Alison     Mathews    21-MAR-76 21-FEB-86    6661.78 Vancouver  Tester
03   James      Smith      12-DEC-78 15-MAR-90    6544.78 Vancouver  Tester
04   Celia      Rice       24-OCT-82 21-APR-99    2344.78 Vancouver  Manager
05   Robert     Black      15-JAN-84 08-AUG-98    2334.78 Vancouver  Tester
06   Linda      Green      30-JUL-87 04-JAN-96    4322.78 New York   Tester
07   David      Larry      31-DEC-90 12-FEB-98    7897.78 New York   Manager
08   James      Cat        17-SEP-96 15-APR-02    1232.78 Vancouver  Tester

8 rows selected.

SQL>
SQL>
SQL>
SQL>
SQL> SET SERVEROUTPUT ON
SQL> DECLARE
  2    -- step 1: declare the variables
  3    v_id employee. id%TYPE;
  4    v_name employee.first_name%TYPE;
  5    v_salary employee.salary%TYPE;
  6
  7    -- step 2: declare the cursor
  8    CURSOR cv_employee_cursor IS
  9      SELECT id, first_name, salary
 10      FROM employee
 11      ORDER BY id;
 12
 13  BEGIN
 14
 15    -- step 3: open the cursor
 16    OPEN cv_employee_cursor;
 17
 18    LOOP
 19
 20      -- step 4: fetch the rows from the cursor
 21      FETCH cv_employee_cursor
 22      INTO v_id, v_name, v_salary;
 23
 24      -- exit the loop when there are no more rows, as indicated by
 25      -- the Boolean variable NOTFOUND (= true when
 26      -- there are no more rows)
 27      EXIT WHEN cv_employee_cursor%NOTFOUND;
 28      -- use DBMS_OUTPUT.PUT_LINE() to display the variables
 29      DBMS_OUTPUT.PUT_LINE(
 30        'v_id = ' || v_id || ', v_name = ' || v_name ||
 31        ', v_salary = ' || v_salary
 32      );
 33
 34    END LOOP;
 35
 36    -- step 5: close the cursor
 37    CLOSE cv_employee_cursor;
 38
 39  END;
 40  /
v_id = 01, v_name = Jason, v_salary = 1234.56
v_id = 02, v_name = Alison, v_salary = 6661.78
v_id = 03, v_name = James, v_salary = 6544.78
v_id = 04, v_name = Celia, v_salary = 2344.78
v_id = 05, v_name = Robert, v_salary = 2334.78
v_id = 06, v_name = Linda, v_salary = 4322.78
v_id = 07, v_name = David, v_salary = 7897.78
v_id = 08, v_name = James, v_salary = 1232.78

PL/SQL procedure successfully completed.

SQL>
SQL>
SQL>
SQL>
SQL> -- clean the table
SQL> drop table Employee
  2  /

Table dropped.








25.1.Introduction
25.1.1.Cursors
25.1.2.First Cursor Example
25.1.3.An example of opening the cursorValue cursor
25.1.4.OPEN Cursor for fetching
25.1.5.A Cursor for counting
25.1.6.To prepare the comma-separated list
25.1.7.Create a cursor for update
25.1.8.An example of cursor variable assignment
25.1.9.Assigning different queries to the same cursor variable
25.1.10.Cursor to reference whole table
25.1.11.select first row to a cursor
25.1.12.Nested cursor
25.1.13.Cursor performance