Upload
lauren-griffin
View
234
Download
0
Tags:
Embed Size (px)
Citation preview
IMPLICIT AND AN EXPLICIT CURSOR
Prepared by Tahani Alahmadi
Objectives
After completing this lecture, you should be able to do
the following:• Distinguish between an implicit and an explicit
cursor• Discuss when and why to use an explicit cursor• Declare and control explicit cursors• Use simple loop and cursor FOR loop to fetch data• Declare and use cursors with parameters• Lock rows using the FOR UPDATE clause• Reference the current row with the WHERE CURRENTclause
About Cursors
Every SQL statement executed by the Oracle Server
has an individual cursor associated with it:
• Implicit cursors: Declared and managed by
PL/SQL for all DML and PL/SQL SELECT statements
• Explicit cursors: Declared and managed by the
programmer
Explicit Cursor Operations
Explicit Cursor Operations You declare explicit cursors in PL/SQL when
you have a SELECT statement returning multiple rows. You can process each row returned by the SELECT statement.
The set of rows returned by a multiple-row query is called the active set. Its size is the number of rows that meet your search criteria. The diagram in the slide before shows how an explicit cursor “points” to the current row in the active set. This allows your program to process the rows one at a time.
Explicit Cursor functions
Can process beyond the first row returned by the query, row by row
Keep track of which row is currently being processed
Allow the programmer to manually control explicit cursors in the PL/SQL block
Controlling Explicit Cursors
How to use Explicit Cursor?
There are four steps in using an Explicit Cursor. 1.
DECLARE the cursor in the declaration section.2. OPEN the cursor in the Execution Section. FETCH the data from cursor into PL/SQL variables or
records in the Execution Section. In the flow diagram shown in the slide before, after each fetch you test the cursor for any existing row. If there are no more rows to process, then you must close the cursor.
1. CLOSE the cursor in the Execution Section before end the PL/SQL Block. The CLOSE statement releases the active set of rows.
How to access an Explicit Cursor?
These are the three steps in accessing the cursor.
1) Open the cursor.General Syntax to open a cursor is:OPEN cursor_name;
2) Fetch the records in the cursor one at a time.General Syntax to fetch records from a cursor is:FETCH cursor_name INTO record_name;OR FETCH cursor_name INTO variable_list;
3) Close the cursor.General Syntax to close a cursor is:CLOSE cursor_name;
Controlling Explicit Cursors
Controlling Explicit Cursors1. The OPEN statement executes the query associated with the cursor, identifies the activeset, and positions the cursor to the first row.2. The FETCH statement retrieves the current row and advances the cursor to the next row until either there are no more rows or until a specified condition is met.3. The CLOSE statement releases the cursor.
Declaring the Cursor* SELECT statement without an INTO clause* If processing rows in a specific sequence is required, use the ORDER BY clause in thequery.
Opening the Cursor
Fetching Data from the Cursor
Fetching Data from the Cursor
Fetching Data from the Cursor
Fetching Data from the Cursor
Closing the Cursor
Example
1> DECLARE 2> emp_rec emp_tbl%rowtype;3> CURSOR emp_cur IS 4> SELECT *5> FROM emp_tbl6> WHERE salary > 10; 7> BEGIN 8> OPEN emp_cur; 9> FETCH emp_cur INTO emp_rec; 10> dbms_output.put_line (emp_rec.first_name || ' ' || emp_rec.last_name); 11> CLOSE emp_cur; 12> END;
The %ROWTYPE attribute provides a record type that represents a row in a database table. The record can store an entire row of data selected from the table or fetched from a cursor or cursor variable. Variables declared using %ROWTYPE are treated like those declared using a datatype name. You can use the %ROWTYPE attribute in variable declarations as a datatype specifier.Fields in a record and corresponding columns in a row have the same names and datatypes.
Example
In the above example, first we are creating a record ‘emp_rec’ of the same
structure as of table ‘emp_tbl’ in line no 2. Second, we are declaring a cursor ‘emp_cur’ from a
select query in line no 3 - 6. Third, we are opening the cursor in the execution
section in line no 8. Fourth, we are fetching the cursor to the record in
line no 9. Fifth, we are displaying the first_name and last_name
of the employee in the record emp_rec in line no 10. Sixth, we are closing the cursor in line no 11.
Cursors and Records
Process the rows of the active set by fetching values into a PL/SQL RECORD.
Cursor FOR Loops
• The cursor FOR loop is a shortcut to processexplicit cursors.• Implicit open, fetch, exit, and close occur.• The record is implicitly declared.
Cursor FOR Loops
You have learned to fetch data from cursors by using simple loops. You will now learn to use a cursor FOR loop. A cursor FOR loop processes rows in an explicit cursor. It is a shortcut because the cursor is opened, a row is fetched once for each iteration in the loop, the loop exits when the last row is processed, and the cursor is closed automatically. The loop itself is terminated automatically at the end of the iteration where the last row is fetched.In the syntax: record_name Is the name of the implicitly declared
record cursor_name Is a PL/SQL identifier for the previously
declared cursor
Cursor FOR Loops Guidelines
Do not declare the record that controls the loop because it is declared implicitly.
Test the cursor attributes during the loop, if required.
Supply the parameters for a cursor, if required, in parentheses following the cursor name in the FOR statement.
Cursor FOR Loops
use for loops in cursors
1> DECLARE 2> CURSOR emp_cur IS 3> SELECT first_name, last_name, salary FROM emp_tbl; 4> 5> BEGIN 6> FOR emp_rec in emp_cur 7> LOOP 8> dbms_output.put_line(emp_cur.first_name || ' ' ||emp_cur.last_name 9> || ' ' ||emp_cur.salary); 10> END LOOP; 11>END;12> /
use for loops in cursors
when the FOR loop is processed a record ‘emp_rec’of structure ‘emp_cur’ gets created, the cursor is opened, the rows are fetched to the record ‘emp_rec’ and the cursor is closed after the last row is processed. By using FOR Loop in your program, you can reduce the number of lines in the program.
Explicit Cursor Attributes
What are Explicit Cursor Attributes?Oracle provides some attributes known as Explicit Cursor Attributes to control the data processing while using cursors. We use these attributes to avoid errors while accessing cursors through OPEN, FETCH and CLOSE Statements.Note: You cannot reference cursor attributes directly in a SQL statement.
When does an error occur while accessing an explicit cursor?
a) When we try to open a cursor which is not closed in the previous operation.b) When we try to fetch a cursor after the last operation.
Explicit Cursor AttributesObtain status information about a
cursor.
The %ISOPEN Attribute
• Fetch rows only when the cursor is open.• Use the %ISOPEN cursor attribute before performing a fetch to test whether the cursor is open.Example:
Example of %ROWCOUNT and %NOTFOUND
This example in retrieves the first ten employees one by one. It shows how %ROWCOUNT and %NOTFOUND attributes can be used for exit conditions in a loop.
Cursor FOR Loops Using Subqueries
No need to declare the cursor. Example:
Cursor FOR Loops Using Subqueries
Observe that there is no declarative section in this PL/SQL block. The difference between the cursor FOR loops using subqueries and the cursor FOR loop lies in the cursor declaration.
If you are writing cursor FOR loops using subqueries, you need not declare the cursor in the declarative section. You have to provide the SELECT statement that determines the active set in the loop itself.
Note: You cannot reference explicit cursor attributes if you use a subquery in a cursor FOR loop because you cannot give the cursor an explicit name.
Cursors with Parameters
Cursors with Parameters
Summary
In this lecture, you should have learned how to:• Distinguish cursor types:– Implicit cursors: Used for all DML statements
and single-row queries– Explicit cursors: Used for queries of zero, one,
or more rows• Create and handle explicit cursors• Use simple loops and cursor FOR loops to
handle multiple rows in the cursors• Evaluate the cursor status by using the cursor
attributeshttp
://docs.oracle.com/cd/B19306_01/appdev.102/b14261/sqloperations.htm