0% found this document useful (0 votes)
15 views2 pages

2 Cursors

Uploaded by

Sulbha Gath
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views2 pages

2 Cursors

Uploaded by

Sulbha Gath
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Explain Cursor in context to PL/SQL or what is a Cursor in PL/SQL

PL/SQL: Cursors and Their Types

What is a Cursor?

A cursor is like a bookmark you use to keep track of rows in a table. It allows us to go through
rows in a table one by one when we need to process each row individually.

 Implicit Cursors: Easy to use for simple tasks.


 Explicit Cursors: More powerful for row-by-row operations.

Types of Cursors

There are two main types of cursors:

Type Definition Created By


Implicit Automatically created by PL/SQL when a query is run. You Automatically by
don't write extra code for it. PL/SQL
Explicit You create and control it manually. Useful when you need to Created by the
process rows one by one. programmer

Implicit Cursor Example

When you run a simple query like INSERT, UPDATE, or SELECT INTO, PL/SQL automatically
creates an implicit cursor for you.

Example: Count the number of students in a table.

Code:

DECLARE
student_count NUMBER;
BEGIN
SELECT COUNT(*) INTO student_count FROM students;
DBMS_OUTPUT.PUT_LINE('Total students: ' || student_count);
END;

What Happens:

1. PL/SQL creates an implicit cursor for the SELECT statement.


2. The result (total number of students) is stored in student_count.
3. The program prints the total.

Unit: 4 - PL/SQL control statements and stored procedures Dr. Sulbha Gath Page 1
Explicit Cursor Example

Explicit cursors are created when you want to process rows one by one.

Example: Display names of all students.

Code:

DECLARE
CURSOR student_cursor IS
SELECT name FROM students;
student_name students.name%TYPE;
BEGIN
OPEN student_cursor;
LOOP
FETCH student_cursor INTO student_name;
EXIT WHEN student_cursor%NOTFOUND;
DBMS_OUTPUT.PUT_LINE('Student Name: ' || student_name);
END LOOP;
CLOSE student_cursor;
END;

What Happens:

1. CURSOR student_cursor IS SELECT name FROM students; : You define a cursor that
selects student names.
2. OPEN student_cursor;: Opens the cursor and gets it ready to fetch rows.
3. FETCH student_cursor INTO student_name;: Reads one row from the table into the
variable student_name.
4. LOOP ... END LOOP;: Repeats the process until all rows are processed.
5. CLOSE student_cursor;: Closes the cursor when done.

Key Differences Between Implicit and Explicit Cursors

Feature Implicit Cursor Explicit Cursor


Creation Automatically by PL/SQL Manually by the programmer
Control PL/SQL controls it Programmer has full control
Use Simple operations (e.g., SELECT Complex row-by-row operations
INTO)
Code Requires less code Requires more code (OPEN, FETCH,
Complexity CLOSE)

Unit: 4 - PL/SQL control statements and stored procedures Dr. Sulbha Gath Page 2

You might also like