0% found this document useful (0 votes)
28 views34 pages

U6 ORACLE PLSQL 2025 Implicit Cursors Students

The document provides an overview of PL/SQL cursors, which are used to manage SELECT statements and are categorized into implicit and explicit types. Implicit cursors are automatically created for single record queries, while explicit cursors must be declared for multiple records. The document also discusses cursor operations, variable declarations, and examples demonstrating the use of cursors in PL/SQL programming.
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)
28 views34 pages

U6 ORACLE PLSQL 2025 Implicit Cursors Students

The document provides an overview of PL/SQL cursors, which are used to manage SELECT statements and are categorized into implicit and explicit types. Implicit cursors are automatically created for single record queries, while explicit cursors must be declared for multiple records. The document also discusses cursor operations, variable declarations, and examples demonstrating the use of cursors in PL/SQL programming.
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/ 34

PL/SQL.

CURSOR
Knowledge is of two kinds.
We know a subject ourselves, or
We know where we can find information upon it.
(Samuel Jhonson)

cc-by-sa Gema Cervigón. Guadalupe Bermejo UT7 DDBB 1


PL/SQL. CURSOR
● PL/SQL uses cursors to manage SELECT statements. A cursor is a set of records returned
by an SQL statement. Technically cursors are chunks of memory reserved for processing
the results of a SELECT query.
● A cursor is defined like any other PL/SQL variable and must be named according to
the same conventions as any other variable. There are two types of cursors: Implicit and
Explicit.
● Implicit : This type of cursors is used for SELECT INTO operations. They are used when the
query returns a single record and do not need to be declared as such.
○ Whenever Oracle executes an SQL statement such as SELECT INTO, INSERT, UPDATE,
and DELETE, it automatically creates an implicit cursor.
● Explicit : They are used when the query returns a set of records and must be declared
with the word CURSOR. They are also occasionally used in queries that return a single
record for efficiency reasons. They are faster.

cc-by-sa Gema Cervigón. Guadalupe Bermejo UT7 DDBB 2


PL/SQL. CURSOR
● A cursor is a structure that allows access to the context area where the rows
resulting from the associated query are stored.
○ It is not a variable.
○ It cannot be assigned values.
○ It cannot appear in expressions.
○ Its contents can only be read.
○ Every cursor has an associated SELECT statement that is part of its own
definition.

cc-by-sa Gema Cervigón. Guadalupe Bermejo UT7 DDBB 3


PL/SQL. CURSOR
● What happens if the information in the DB changes during the execution of
the program using the cursor?
○ The information accessible by the cursor, in the context area, is not
updated.
○ The cursor is like a still photo, of a part of the DB at a given moment.
○ That moment is the opening moment of the cursor, that is, the instant at
which the SELECT associated with the cursor is executed.

cc-by-sa Gema Cervigón. Guadalupe Bermejo UT7 DDBB 4


PL/SQL. CURSOR

cc-by-sa Gema Cervigón. Guadalupe Bermejo UT7 DDBB 5


PL/SQL. CURSOR
● Types of cursors:
○ Implicit
○ Explicit
● Operations for their manipulation:
○ Opening
○ Reading
○ Close
● PL/SQL performs these operations automatically in the case of implicit
cursors.

cc-by-sa Gema Cervigón. Guadalupe Bermejo UT7 DDBB 6


PL/SQL. Implicit CURSORS
● They do not have to be explicitly declared before use.
● They can only store one row which is the result of the SELECT statement
associated with them. If the query returns more than one row, it will return
an error.
● Sintax:
SELECT <column/s> | *
INTO <variable/s>
FROM <table/s> [WHERE...];

cc-by-sa Gema Cervigón. Guadalupe Bermejo UT7 DDBB 7


PL/SQL. Implicit CURSORS
● Variables store the result value of the query, obtained through the cursor.
● Variables type must be compatible with the type of the columns specified in
the SELECT.
● Variables must be declared before they can be referenced in the query.
● For their definition it is recommended to use the attributes:
● %TYPE, for the definition of columns Why?
● %ROWTYPE, for the definition of a row
● Variables can be named in the same way as the columns of the tables used in
the query.

cc-by-sa Gema Cervigón. Guadalupe Bermejo UT7 DDBB 8


PL/SQL. Implicit CURSORS .. %TYPE
PL/SQL allows to declare a variable whose data type anchor to a table column or another
variable. Consider the following example to show name and credit limit of customer 38:
CREATE OR REPLACE PROCEDURE Show38 IS
DECLARE
l_customer_name customers.name%TYPE;
l_credit_limit customers.credit_limit%TYPE;
BEGIN
SELECT
name, credit_limit SELECT name, credit_limit
INTO FROM customers
l_customer_name, l_credit_limit
WHERE customer_id = 38;
FROM
customers
WHERE
customer_id = 38;

DBMS_OUTPUT.PUT_LINE(l_customer_name || ':' || l_credit_limit );


END Show38;

cc-by-sa Gema Cervigón. Guadalupe Bermejo UT7 DDBB 9


PL/SQL. Implicit CURSORS .. %TYPE
PL/SQL allows you to declare a variable whose data type anchor to a table
column or another variable. Consider the following example:
DECLARE
l_customer_name customers.name%TYPE;
l_credit_limit customers.credit_limit%TYPE;

First, declare two variables l_customer_name and l_credit_limit whose


data type anchors to the name and credit_limit columns respectively, in
the declaration section of the block.

cc-by-sa Gema Cervigón. Guadalupe Bermejo UT7 DDBB 10


PL/SQL. Implicit CURSORS .. %TYPE
PL/SQL allows you to declare a variable whose data type anchor to a table column or another
variable. Consider the following example:

BEGIN
SELECT
name, credit_limit
INTO
l_customer_name, l_credit_limit
FROM
customers
WHERE
customer_id = 38;

Second, query customer_name and credit_limit of the customer_id 38 and assign these
column values to the l_customer_name and l_credit_limit variables in the execution block.

cc-by-sa Gema Cervigón. Guadalupe Bermejo UT7 DDBB 11


PL/SQL. Implicit CURSORS .. %TYPE
PL/SQL allows you to declare a variable whose data type anchor to a table
column or another variable. Consider the following example:
DBMS_OUTPUT.PUT_LINE(l_customer_name || ':' || l_credit_limit );
END;
/
Third, display the customer name and credit limit.

cc-by-sa Gema Cervigón. Guadalupe Bermejo UT7 DDBB 12


PL/SQL. Implicit CURSORS .. %TYPE
1. Calculate the total amount of employees and the average of their salaries. We will
use the command %TYPE to assign a value to a variable from a select clause.
CREATE OR REPLACE PROCEDURE Average_Salaries IS

numemple NUMBER(3);
average_salary Emp.sal%TYPE;
BEGIN
SELECT COUNT(*), AVG(sal)
INTO numemple, average_salary FROM Emp;
DBMS_OUTPUT.PUT_LINE('Number of employees on Emp table = ' || numemple);
DBMS_OUTPUT.PUT_LINE('Employees average salary on Emp table = '|| average_salary);

END Average_Salaries;

cc-by-sa Gema Cervigón. Guadalupe Bermejo UT7 DDBB 13


PL/SQL. Implicit CURSORS .. %TYPE
1. Calculate the total amount of employees and the average of their salaries. We will
use the command %TYPE to assign a value to a variable from a select clause.
CREATE OR REPLACE PROCEDURE Average_Salaries IS
numemple Emp.empno%TYPE;
average_salary Emp.sal%TYPE;
BEGIN
SELECT COUNT(*), AVG(sal)
INTO numemple, average_salary FROM Emp;
DBMS_OUTPUT.PUT_LINE('Number of employees on Emp table = ' || numemple);
DBMS_OUTPUT.PUT_LINE('Employees average salary on Emp table = '|| average_salary);
END;

cc-by-sa Gema Cervigón. Guadalupe Bermejo UT7 DDBB 14


PL/SQL. Implicit CURSORS .. %TYPE
2. Write a PL/SQL block to show the salary of the employee Jones (emp table)

DECLARE
sal emp.sal%TYPE;
BEGIN
SELECT sal INTO sal
FROM emp
WHERE ename= 'JONES';
DBMS_OUTPUT.PUT_LINE ('Jones earns: ' || sal );
END;
/
Jones earns: 2975

cc-by-sa Gema Cervigón. Guadalupe Bermejo UT7 DDBB 15


PL/SQL. Implicit CURSORS .. %TYPE
3. Write a PL/SQL block to show the name and job of the president

DECLARE
name Emp.ename%TYPE;
job Emp.job%TYPE;
BEGIN
SELECT ename, job
INTO name, job FROM Emp
WHERE job = 'PRESIDENT';
DBMS_OUTPUT.PUT_LINE ( name|| ' is the ' || job);
END;
/

cc-by-sa Gema Cervigón. Guadalupe Bermejo UT7 DDBB 16


PL/SQL. Implicit CURSORS .. %ROWTYPE
PL/SQL allows us to declare a variable whose data type anchor to a table row of a
table.
Special notations:
● Table.Column%TYPE: The type assigned to a column of a table, regardless of
how it is defined.
● Table%ROWTYPE: The type that a row of a table is assigned to, regardless of
how the table is defined.

cc-by-sa Gema Cervigón. Guadalupe Bermejo UT7 DDBB 17


PL/SQL. Implicit CURSORS .. %ROWTYPE
Example 1 Note

We can create a record indicating the data type of each field.


In order not to have problems with the
DECLARE INSERT statement, we must have
TYPE RegPerson IS RECORD previously created a table whose rows (or
( records) are identical to the Pepe record.
Codigo NUMBER ( 2 ),
Nombre VARCHAR2 ( 40 ),
Edad NUMBER For example:
);
Person RegPerson; CREATE TABLE People
BEGIN
Person.Codigo := 1 ; (
Person.Nombre := 'Pepe' ; CODE NUMBER ( 2 ),
Person.Edad := 30 ;
DBMS_OUTPUT.PUT_LINE ( 'Code: ' || Person.Codigo ); NAME VARCHAR2 ( 40 ),
DBMS_OUTPUT.PUT_LINE ( 'Name: ' || Person.Nombre ); AGE NUMBER
DBMS_OUTPUT.PUT_LINE ( 'Age: ' || Person.Edad );
INSERT INTO People VALUES Person; );
END;
/

cc-by-sa Gema Cervigón. Guadalupe Bermejo UT7 DDBB 18


PL/SQL. Implicit CURSORS .. %ROWTYPE
Example 2 (Using %TYPE attribute)

We can create a record indicating that the data type of each field matches fields of a table.

DECLARE
TYPE RegPerson IS RECORD
(
CODE People.Code%TYPE,
NAME People.Name%TYPE,
AGE People.Age%TYPE
);
Person RegPerson ; Test it
yourself

Code: 1
Name: Pepe
Age: 30 cc-by-sa Gema Cervigón. Guadalupe Bermejo UT7 DDBB 19
PL/SQL. Implicit CURSORS .. %ROWTYPE
Example 3 (Using %ROWTYPE attribute)

We can indicate that a record variable matches the row format of a table. The above example can be simplified like this:

DECLARE
Person People%ROWTYPE ; -- Person is an implicit cursor
BEGIN
SELECT * INTO Person
FROM People
WHERE code = 1 ; -- Stores a single record
DBMS_OUTPUT.PUT_LINE ( 'Person code: ' || Person.CODE );
DBMS_OUTPUT.PUT_LINE ( 'Person name: ' || Person.NAME) ;
END ;
/

Person code: 1
OR
ERROR at line 70:
Person name: Pepe ORA-01422: exact fetch returns more than requested number of rows

cc-by-sa Gema Cervigón. Guadalupe Bermejo UT7 DDBB 20


PL/SQL. Implicit CURSORS .. %ROWTYPE
DECLARE
Person People%ROWTYPE ;
BEGIN
SELECT * INTO Person
FROM People
WHERE code = 1 ;
DBMS_OUTPUT.PUT_LINE ( 'Person code: ' || Person.CODE );
DBMS_OUTPUT.PUT_LINE ( 'Person name: ' || Person.NAME) ;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('*** This person doesn’t exist ');
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('***Error during execution ');
END;
/

cc-by-sa Gema Cervigón. Guadalupe Bermejo UT7 DDBB 21


PL/SQL. Implicit CURSORS .. %ROWTYPE
4. Write a PL/SQL block to show, the data of employee nº 7900 (from emp).
DECLARE
emple Emp%ROWTYPE;
BEGIN
SELECT * INTO emple
FROM Emp WHERE empno=7900;
DBMS_OUTPUT.PUT_LINE
(emple.ename || ' ' ||
emple.job ||' '||
emple.mgr ||' '||
emple.hiredate ||' '||
emple.comm ||' '||
emple.deptno);
END;
/

JAMES CLERK 7698 03/12/81 30

cc-by-sa Gema Cervigón. Guadalupe Bermejo UT7 DDBB 22


PL/SQL. Implicit CURSORS
5. Create a PL/SQL program that prints on the screen the amount of money spent
on salaries by the company.
DECLARE
total int;
BEGIN
SELECT sum(sal)
INTO total FROM Emp;
DBMS_OUTPUT.PUT_LINE('Money spent on salaries: ' || total);
END;
/

cc-by-sa Gema Cervigón. Guadalupe Bermejo UT7 DDBB 23


PL/SQL. Implicit CURSORS
The same cursor using &TYPE
int?
DECLARE number(7,2)?
other?
total emp.sal%TYPE;
BEGIN
SELECT sum(sal)
INTO total FROM Emp;
DBMS_OUTPUT.PUT_LINE('Money spent on salaries: ' || total);
END;
/

cc-by-sa Gema Cervigón. Guadalupe Bermejo UT7 DDBB 24


PL/SQL. Implicit CURSORS
6. Create a PL/SQL block that prints on the screen the amount of money spent
on commissions by the company.

DECLARE
total int;
BEGIN
SELECT sum(comm)
INTO total FROM Emp;
DBMS_OUTPUT.PUT_LINE('Total commissions: ' || total);
END;
/

cc-by-sa Gema Cervigón. Guadalupe Bermejo UT7 DDBB 25


PL/SQL. Implicit CURSORS
7. Create a PL/SQL block to update the employees (emp table) earning less than the average commission. The
new commission will be the average of the commissions. Remember that at the end of the changes, in order
for the table to be updated, we must use the commit command.
SELECT ename, comm FROM Emp WHERE comm IS NOT NULL; -- ALLEN 300,
WARD 500,
MARTIN 1400
TURNER 0
SELECT AVG(comm) FROM Emp; -- 550
SELECT ename, comm FROM Emp
WHERE comm < (SELECT AVG(comm) FROM Emp); -- ALLEN 300,
WARD 500,
TURNER 0
SELECT AVG(comm) FROM Emp
WHERE comm < (SELECT AVG(comm) FROM Emp); -- (300+500+0)/3 = 266,66

cc-by-sa Gema Cervigón. Guadalupe Bermejo UT7 DDBB 26


PL/SQL. Implicit CURSORS
7. Create a PL/SQL block to update the employees (emp table) earning less than the average
commission. The new commission will be the average of the commissions. Remember that at the
end of the changes, in order for the table to be updated, we must use the commit command.

DECLARE
comm int; SELECT ename, comm
BEGIN FROM Emp
UPDATE Emp
WHERE comm IS NOT
SET comm = (SELECT AVG(comm) FROM Emp)
WHERE comm < (SELECT AVG(comm) FROM Emp);
NULL;
-- ROLLBACK; ALLEN 550,
-- COMMIT;
WARD 550,
END;
/ MARTIN 1400
TURNER 550
cc-by-sa Gema Cervigón. Guadalupe Bermejo UT7 DDBB 27
PL/SQL. Implicit CURSORS
7. Create a PL/SQL block to update the employees (emp table) earning less than the average
commission. The new commission will be the average of the commissions. Remember that at the
end of the changes, in order for the table to be updated, we must use the commit command.

DECLARE
comm EMP.comm%TYPE;
SELECT ename, comm
BEGIN FROM Emp
UPDATE Emp
WHERE comm IS NOT
SET comm = (SELECT AVG(comm) FROM Emp)
NULL;
WHERE comm < (SELECT AVG(comm) FROM Emp);
-- ROLLBACK; ALLEN 550,
-- COMMIT;
WARD 550,
END;
/ MARTIN 1400
TURNER 550
cc-by-sa Gema Cervigón. Guadalupe Bermejo UT7 DDBB 28
PL/SQL. Implicit CURSORS
8. Count the total number of employees and the number of employees working as CLERK, SALESMAN, ANALYST and MANAGER. Check that the sum of the partial totals
match the total number of employees in the table.
DECLARE
total_empl NUMBER(3);
totalCLERK NUMBER(3);
totalSALESMAN NUMBER(3);
totalANALYST NUMBER(3);
totalMANAGER NUMBER(3);
checked NUMBER (3);
BEGIN
SELECT COUNT(*) INTO total_empl FROM Emp;
SELECT COUNT(*) INTO totalCLERK FROM Emp WHERE job='CLERK';
SELECT COUNT(*) INTO totalSALESMAN FROM Emp WHERE job='SALESMAN';
SELECT COUNT(*) INTO totalANALYST FROM Emp WHERE job='ANALYST';
SELECT COUNT(*) INTO totalMANAGER FROM Emp WHERE job='MANAGER';
DBMS_OUTPUT.PUT_LINE('Total employees '|| total_empl);
DBMS_OUTPUT.PUT_LINE('total CLERK '|| totalCLERK);
DBMS_OUTPUT.PUT_LINE('total SALESMAN '|| totalSALESMAN);
DBMS_OUTPUT.PUT_LINE('total ANALYST '|| totalANALYST);
DBMS_OUTPUT.PUT_LINE('total MANAGER '|| totalMANAGER);
checked:=(totalCLERK + totalSALESMAN + totalANALYST + totalMANAGER);
DBMS_OUTPUT.PUT_LINE('Total check ' || checked);
IF (checked = total_empl) THEN
DBMS_OUTPUT.PUT_LINE('The total number of employees is equal to the total sum of partial n. of employees ');
ELSE
DBMS_OUTPUT.PUT_LINE('The total number of employees does not match the sum of partial n. of employees ');
END IF;
END;
/

cc-by-sa Gema Cervigón. Guadalupe Bermejo UT7 DDBB 29


PL/SQL. Implicit CURSORS
8. Count the total number of employees and the number of employees working as
CLERK, SALESMAN, ANALYST and MANAGER. Check that the sum of the partial
totals match the total number of employees in the table.

Total employees 14
total CLERK 4
total SALESMAN 4
total ANALYST 2
total MANAGER 3
Total check 13
The total number of employees does not match the sum of partial n. of employees

Why the totals do not match? Solve the problem.

cc-by-sa Gema Cervigón. Guadalupe Bermejo UT7 DDBB 30


PL/SQL. Implicit CURSORS
9. We want to increase the salary of employees working as CLERK in 75 euros, if
the average of their salary is lower than the average of employees working as
SALESMAN.
SELECT sal FROM Emp WHERE job='CLERK';

SELECT AVG(sal) FROM Emp WHERE job='SALESMAN'; -- 1400

cc-by-sa Gema Cervigón. Guadalupe Bermejo UT7 DDBB 31


PL/SQL. Implicit CURSORS
9. We want to increase the salary of employees working as CLERK in 75 euros, if the average of their salary is lower
than the average of employees working as SALESMAN.

DECLARE SELECT sal


mediaCLERK NUMBER; FROM Emp
mediaSALESMAN NUMBER;
BEGIN WHERE job=’CLERK’;
SELECT AVG(sal) INTO mediaCLERK
FROM Emp WHERE job= 'CLERK';
SELECT AVG(sal) INTO mediaSALESMAN
FROM Emp WHERE job= 'SALESMAN';
IF mediaCLERK < mediaSALESMAN
THEN UPDATE Emp SET sal=sal+75 WHERE job='CLERK';
END IF;
-- COMMIT;
-- ROLLBACK;
END;
/

cc-by-sa Gema Cervigón. Guadalupe Bermejo UT7 DDBB 32


PL/SQL. Implicit CURSORS
9.1. We want to increase the salary of employees working as CLERK in 75 euros, if
the average of their salary is lower than the average of employees working as
SALESMAN. Display the new salaries of the employees, after the update, and then
restore the previous state. Imagine that this is a simulation to find out which
employees would increase their salary.

cc-by-sa Gema Cervigón. Guadalupe Bermejo UT7 DDBB 33


PL/SQL. Implicit CURSORS
Advantages:
● We don’t need to declare them.
● They are very easy to use.
Disadvantages:
● They only support one row as a result, which forces to continuously query the
DB to obtain information about other necessary rows.
Explicit cursors were developed to overcome these limitations.

cc-by-sa Gema Cervigón. Guadalupe Bermejo UT7 DDBB 34

You might also like