Вы находитесь на странице: 1из 39

Release Date: August, 2016

Updates:
2
3
4
5
6
7
A SQL cursor is a private Oracle SQL working area. The implicit cursor is used by the server to test and
parse the SQL statements. Using implicit cursors, we can test the outcome of SQL statements in PL/SQL.

An implicit cursor is always called SQL, and we can access its attributes: SQL%FOUND, SQL%NOTFOUND
and SQL%ROWCOUNT.

You can use explicit cursors to name a context area and access its stored data.

8
To limit the SELECT statement so it returns only one row, the WHERE clause must be matching on either
the primary key or other unique column. For example:

SELECT
FROM employees
WHERE employee_id = 101;

Group functions without a GROUP BY clause also return exactly one row. For example:

SELECT COUNT(*) INTO v_number_of_employees


FROM employees;

In all situations where the SELECT statement may return more than one row, and in situations where
multiple rows are needed for processing, programmers should declare and use an explicit cursor.

9
As mentioned earlier, the point is not whether there is more than one row, but that there can be more
than one row. Programmers must think about the data that is possible as well as the data that actually
exists now in order to minimize the need to modify the program in the future.

Explicit cursors are a great tool to make programming in PL/SQL easier and faster. For queries that return
more than one row, you can explicitly declare a cursor to hold the data and then process the rows
individually.

It is possible in PL/SQL to write a SELECT statement which returns more than one row, without using
explicit cursors. This technique is called bulk binding, but it is beyond the scope of this course. Also, bulk
binding is not always possible, so we still need to understand and use explicit cursors.

10
As with variables to be used in the block of code, cursors are defined in the declarative section. To access
the data represented by the cursor definition, you must first OPEN the cursor, then FETCH the current row
from the cursor in order to process the data in that row. When all required data is processed and/or the
cursor is no longer needed, you must CLOSE the cursor.

The meaning and use of the keywords CURSOR, OPEN, FETCH, and CLOSE will be explained and
demonstrated over the next several slides.

11
The name and structure of the cursor is defined in the declarative section using the CURSOR keyword. The
OPEN statement executes the SELECT statement found in the CURSOR definition and populates the active
set with the results of the SELECT.

12
13
The FETCH statement retrieves the current row and advances the cursor to the next row until the last row
is retrieved or until a specified condition is met.

14
15
16
17
Remember, the ORDER BY clause is last. SQL SELECT statements can be complexespecially with joins and
subqueries combined. The code needs to be written so it is readable and understandable. Along with these
guidelines, remember to indent, capitalize keywords, and include empty space/blank lines for readability.

18
In the executable section, a single-row SELECT statement must have an INTO clause. However, a SELECT
statement in a cursor declaration cannot have an INTO clause. This is because you are not retrieving the
data into variables yet (that happens in the FETCH statement).

19
Note that even if there is currently only one employee in department #30, we should still declare a cursor,
because in the future there may be more than one employee.

20
21
22
23
24
25
26
As you can see on the slide, we can use explicit cursor attributes to test for the outcome of the FETCH. Here
we are using cur_emps%NOTFOUND to exit the LOOP.

27
Data type mismatching is the most common mistake that programmers make when writing their code. Data
types must be compatible between the table and the variables. Use of %TYPE is helpful in eliminating data
type mismatches.

The variables in the FETCH statement must line up with the columns in the cursor, so make sure to have the
same number of variables in your INTO statement as you have in your SELECT list. Loops are typically used
to fetch data from cursors. The %NOTFOUND cursor attribute is typically used to stop the loop.

28
29
Answer: The SELECT statement in the cursor declaration returns three columns, but the FETCH statement
references only two variables.

30
Answer: There needs to be an EXIT WHEN added after the FETCH line. The OPEN statement populates the
active set with exactly one row. The first time through the loop, that row will be fetched and displayed. The
second (and third, and fourth...) times through the loop, no test is made for the end of the cursor.
Therefore, the first row will be displayed over and over again (or possibly a time-out type error may occur
since the result of the code is an infinite loop).

31
Once a CURSOR is CLOSEd, the memory is released. But, the data is still in memory and will be overwritten
when a new OPEN statement is executed. So, if the CURSOR is no longer needed, the memory can be
released.
A cursor can be reopened only if it is closed.

32
Once a CURSOR is CLOSEd, the memory is released. But, the data is still in memory and will be overwritten
when a new OPEN statement is executed. So, if the CURSOR is no longer needed, the memory can be
released.
A cursor can be reopened only if it is closed.

33
Because a context area is a private memory area, any changes to database data (DML statements)
executed by other users will not be visible to our cursor until we close and reopen the cursor.

34
35
Active set The set of rows returned by a multiple row query in an explicit cursor operation.
CLOSE Disables a cursor, releases the context area, and undefines the active set.
Context Area An allocated memory area used to store the data processed by a SQL statement.
Cursor A label for a context area or a pointer to the context area.
Explicit Cursor Declared by the programmer for queries that return more than one row.
FETCH Statement that retrieves the current row and advances the cursor to the next row either until
there are no more rows or until a specified condition is met.
Implicit Cursor Defined automatically by Oracle for all SQL DML statements.
OPEN Statement that executes the query associated with the cursor, identifies the active set, and
positions the cursor pointer to the first row.

36
37
38

Вам также может понравиться