0% found this document useful (0 votes)
3 views

database-l4-sql-plsql

The document provides an overview of SQL and PL/SQL, focusing on the concepts of context areas and cursors in Oracle. It explains the differences between implicit and explicit cursors, detailing their declaration, opening, fetching, and closing processes. Additionally, it includes examples of PL/SQL blocks that utilize cursors to retrieve and display data from a database.

Uploaded by

ishimwelukaku08
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

database-l4-sql-plsql

The document provides an overview of SQL and PL/SQL, focusing on the concepts of context areas and cursors in Oracle. It explains the differences between implicit and explicit cursors, detailing their declaration, opening, fetching, and closing processes. Additionally, it includes examples of PL/SQL blocks that utilize cursors to retrieve and display data from a database.

Uploaded by

ishimwelukaku08
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

lOMoARcPSD|53448331

Database L4 - SQL+ PL/SQL

Database Management (Tunis Business School)

Scan to open on Studocu

Studocu is not sponsored or endorsed by any college or university


Downloaded by DJ ELECT P ([email protected])
lOMoARcPSD|53448331

Downloaded by DJ ELECT P ([email protected])


lOMoARcPSD|53448331

Introduction
• To process an SQL statement, ORACLE needs to create
an area of memory known as the context area.
• Context area
• is a memory area inside the PGA.
• helps Oracle server in processing an SQL statement by
holding the important information about that statement.
• contains information about
• rows returned by a query
• the number of rows processed by a query
• a pointer to the parsed query in the shared pool
2

Downloaded by DJ ELECT P ([email protected])


lOMoARcPSD|53448331

• A cursor is a handle, or a pointer, to the context area.

• Through the cursor, a PL/SQL program can control the


context area and what happens to it as the statement
is processed.

• Two important features about the cursor are


1. Cursors allow you to fetch and process rows
returned by a SELECT statement, one row at a
time.
2. A cursor is named so that it can be referenced. 3

Downloaded by DJ ELECT P ([email protected])


lOMoARcPSD|53448331

Example: Write a PL-SQL block to display pilots’ names whose


salaries are greater than 2200 .

Solution?

SELECT name
This solution is not valid
INTO v_name because it can not assign
FROM Pilot multiple values into a single
WHERE sal> 2200; variable.

Downloaded by DJ ELECT P ([email protected])


lOMoARcPSD|53448331

Cursors

Implicit cursors Explicit cursors


- Automatically declared by - Defined by the program for any
Oracle every time an SQL SQL statement that returns
statement is executed. more than one row of data.
- That means the programmer
- Programmers will not be has declared the cursor within
aware of this happening and the PL/SQL declaration block.
cannot control the implicit 5
cursors and the information in
an implicit cursor.

Downloaded by DJ ELECT P ([email protected])


lOMoARcPSD|53448331

• Any given PL/SQL block issues an implicit cursor whenever an SQL


statement is executed, as long as an explicit cursor does not exist
for that SQL statement.

• An implicit cursor is automatically associated with every SELECT


INTO and DML (Data Manipulation) statement (UPDATE, DELETE,
INSERT and MERGE).

• All UPDATE and DELETE statements have cursors that identify the
set of rows that will be affected by the operation.

• An INSERT statement needs a place to receive the data that is to be


inserted in the database; the implicit cursor fulfills this need. 6

• The most recently opened cursor is called the “SQL%” Cursor.


Downloaded by DJ ELECT P ([email protected])
lOMoARcPSD|53448331

• During the processing of an implicit cursor, Oracle automatically


performs the OPEN, FETCH, and CLOSE operations.
• An implicit cursor cannot tell you how many rows were affected by
an update.
• SQL%ROWCOUNT returns the numbers of rows updated.
Example

BEGIN
UPDATE Pilot
SET Sal= 10 000
WHERE name LIKE 'B%';
DBMS_OUTPUT.PUT_LINE(SQL%ROWCOUNT);
END;
7

Downloaded by DJ ELECT P ([email protected])


lOMoARcPSD|53448331

• The only mean of generating an explicit cursor is for the


cursor to be named in the DECLARE section of the PL/SQL
block.
• The advantages of declaring an explicit cursor over the
indirect implicit cursor are that the explicit cursor gives more
control to the programmer.
• Implicit cursors are less efficient than explicit cursors, and
thus it is harder to trap data errors.

Downloaded by DJ ELECT P ([email protected])


lOMoARcPSD|53448331

• The process of working with an explicit cursor consists of the


following steps

Declare

Open

Fetch

Close 9

Downloaded by DJ ELECT P ([email protected])


lOMoARcPSD|53448331

• The process of working with an explicit cursor consists of the


following steps:

1) DECLARING the cursor. This initializes the cursor into memory.


2) OPENING the cursor. The previously declared cursor can now
be opened. Memory is allotted.
3) FETCHING the cursor. The previously declared and opened
cursor can now retrieve data; this is the process of fetching
the cursor.
4) CLOSING the cursor. The previously declared, opened, and
fetched cursor must now be closed to release memory
allocation. 10

Downloaded by DJ ELECT P ([email protected])


lOMoARcPSD|53448331

• Declaring a cursor defines the name of the cursor and


associates it with a SELECT statement.
• Cursor names follow the same rules of scope and visibility
that apply to the PL/SQL identifiers.
• Because the name of the cursor is a PL/SQL identifier, it must
be declared before it is referenced.
Syntax

CURSOR c_cursor_name IS query_statement ;

Example
DECLARE
CURSOR C1 IS 11
SELECT name FROM pilot WHERE sal<1200 ;

Downloaded by DJ ELECT P ([email protected])


lOMoARcPSD|53448331

• Cursor can also be defined using parameters. These


parameters can be used in a selection condition or in an
expression
Syntax
CURSOR c_cursor_name (parameter_name type [ :=default value] […])
IS query_statement ;

Example

DECLARE
CURSOR C2 (psal NUMBER(8,2), pcom NUMBER(8,2))
IS SELECT name
12
FROM pilot
WHERE sal<psal AND comm> pcom ;

Downloaded by DJ ELECT P ([email protected])


lOMoARcPSD|53448331

• When you open a cursor, the memory will be allotted to it.


• This is done in the BEGIN block section.
Syntax

OPEN c_cursor_name (parameter_name);


Example
DECLARE
CURSOR pilot_nice IS
SELECT nopilot, name
FROM pilot
WHERE address='Nice';
BEGIN
OPEN pilot_nice;
13
...
END ;

Downloaded by DJ ELECT P ([email protected])


lOMoARcPSD|53448331

• After the cursor has been declared and opened, you


can then retrieve data from it.
• The process of retrieving the data from the cursor is
called fetching.
Syntax

FETCH c_cursor_name INTO PL/SQL variable;


Or

FETCH c_cursor_name INTO PL/SQL record;


14

Downloaded by DJ ELECT P ([email protected])


lOMoARcPSD|53448331

When the cursor is fetched, the following occurs:


1. The fetch command is used to retrieve one row at a
time from the active set. This is generally done inside
a loop. The values of each row in the active set can
then be stored into the corresponding variables or
PL/SQL records one at a time, performing operations
on each one successively.

2. After each FETCH, the active set pointer is moved


forward to the next row. Thus, each fetch will return
successive rows of the active set, until the entire set is
returned. The last FETCH will not assign values to the
output variables; they will still contain their prior 15
values.

Downloaded by DJ ELECT P ([email protected])


lOMoARcPSD|53448331

DECLARE
CURSOR pilot_nice IS
SELECT nopilot, name, sal
FROM pilot
WHERE address='Nice';
Pnum pilot.nopilot%TYPE;
Pname pilot.name%TYPE;
Psalary pilot.sal%TYPE;
BEGIN
OPEN pl_nice;
LOOP
FETCH pilot_nice INTO Pnum, Pname, Psalary;
...
EXIT WHEN sal > 10 000; 16
END LOOP;
END

Downloaded by DJ ELECT P ([email protected])


lOMoARcPSD|53448331

• Once all of the rows in the cursor have been processed


(retrieved), the cursor should be closed.
• This tells the PL/SQL engine that the program is finished
with the cursor, and the resources associated with it can be
freed.
Syntax:
CLOSE c_cursor_name;

• Once a cursor is closed, it is no longer valid to fetch from it.


• Likewise, it is not possible to close an already closed cursor
(either one will result in an Oracle error).
17

Downloaded by DJ ELECT P ([email protected])


lOMoARcPSD|53448331

DECLARE
CURSOR pilot_nice IS
SELECT nopilot, name, sal
FROM pilot
WHERE address='Nice';
Pnum pilot.nopilot%TYPE;
Pname pilot.name%TYPE;
Psalary pilot.sal%TYPE;
BEGIN
OPEN pilot _nice;
LOOP
FETCH pilot_nice INTO Pnum, Pname, Psalary;
...
EXIT WHEN Psalary > 10 000;
END LOOP; 18
CLOSE pilot _nice;
END

Downloaded by DJ ELECT P ([email protected])


lOMoARcPSD|53448331

19

Downloaded by DJ ELECT P ([email protected])


lOMoARcPSD|53448331

Write a PL/SQL block to display the names and


salaries of pilots whose income exceed 2200.

20

Downloaded by DJ ELECT P ([email protected])


lOMoARcPSD|53448331

DECLARE
CURSOR C3 IS SELECT name, sal FROM pilot WHERE sal>2200;
V_name pilot.name%TYPE;
V_sal pilot.sal%TYPE;
BEGIN
OPEN C3;
LOOP
FETCH C3 INTO v_name, v_sal;
EXIT WHEN C3%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(‘Pilot name: ' || v_name||' Salary :' || v_sal);
END LOOP;
CLOSE C3;
21
END;

Downloaded by DJ ELECT P ([email protected])


lOMoARcPSD|53448331

DECLARE
TYPE t_pilot IS RECORD (v_name pilot.name%TYPE, v_sal pilot.sal%TYPE);
rec_pilot t_pilot;
CURSOR C3 IS SELECT name,sal FROM pilot WHERE sal>2200;
BEGIN
OPEN C3;
LOOP
FETCH C3 INTO rec_pilot;
EXIT WHEN C3%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(‘Pilot name: ' || rec_pilot.v_name||'
Salary: ' || rec_pilot.v_sal);
END LOOP; 22
CLOSE C3;
END;
Downloaded by DJ ELECT P ([email protected])
lOMoARcPSD|53448331

Solution with scalar variables


with parameterized cursor
DECLARE
--n_sal is the cursor parameter
CURSOR C4 (n_sal pilot.sal%TYPE) IS
SELECT name, sal FROM pilot WHERE sal> n_sal;
V_name pilot.name%TYPE;
V_sal pilot.sal%TYPE;
BEGIN
--input here the actual value of the cursor parameter
OPEN C4 (2200);
LOOP
FETCH C4 INTO v_name, v_sal;
EXIT WHEN C4%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(‘Pilot name: ' || v_name||' Salary :' || v_sal);
END LOOP; 23
CLOSE C4;
END;

Downloaded by DJ ELECT P ([email protected])


lOMoARcPSD|53448331

Solution with scalar variables with


parameterized cursor with default value
DECLARE
--n_sal is the cursor parameter
CURSOR C4 (n_sal pilot.sal%TYPE :=2200) IS
SELECT name, sal FROM pilot WHERE sal> n_sal;
V_name pilot.name%TYPE;
V_sal pilot.sal%TYPE;
BEGIN
OPEN C4;
/*No value was given to the value of the cursor parameter n_sal
Default value 2200 will be used */
LOOP
FETCH C4 INTO v_name, v_sal;
EXIT WHEN C4%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(‘Pilot name: ' || v_name||' Salary :' || v_sal);
END LOOP; 24
CLOSE C4;
END;

Downloaded by DJ ELECT P ([email protected])


lOMoARcPSD|53448331

• Another way that opens a cursor, processes the rows


returned by it, and closes it or is called condensed
form and uses the FOR structure.
• However, the declaration is made in the same way as
before.
• When using the cursor FOR LOOP, the process of
opening, fetching, and closing are implicitly handled.
• This makes the blocks much simpler to code and
easier to maintain.
• The cursor FOR LOOP specifies a sequence of
statements to be repeated once for each row
returned by the cursor.
25
• Use the cursor FOR LOOP if you need to FETCH and
PROCESS each and every record from a cursor.
Downloaded by DJ ELECT P ([email protected])
lOMoARcPSD|53448331

DECLARE
i BINARY_INTEGER ;
CURSOR C3 IS
SELECT name,sal FROM pilot WHERE sal>2200;
V_name pilot.name%TYPE;
V_sal pilot.sal%TYPE;
BEGIN
FOR i IN C3 ;
LOOP
FETCH C3 INTO V_name,V_sal;
DBMS_OUTPUT.PUT_LINE(‘Pilot name: ' || v_name||' Salary :' || v_sal);
END LOOP;
26
END

Downloaded by DJ ELECT P ([email protected])

You might also like