implicit and an explicit cursor prepared by tahani alahmadi

36
IMPLICIT AND AN EXPLICIT CURSOR Prepared by Tahani Alahmadi

Upload: lauren-griffin

Post on 19-Jan-2016

234 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: IMPLICIT AND AN EXPLICIT CURSOR Prepared by Tahani Alahmadi

IMPLICIT AND AN EXPLICIT CURSOR

Prepared by Tahani Alahmadi

Page 2: 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

Page 3: IMPLICIT AND AN EXPLICIT CURSOR Prepared by Tahani Alahmadi

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

Page 4: IMPLICIT AND AN EXPLICIT CURSOR Prepared by Tahani Alahmadi

Explicit Cursor Operations

Page 5: IMPLICIT AND AN EXPLICIT CURSOR Prepared by Tahani Alahmadi

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.

Page 6: IMPLICIT AND AN EXPLICIT CURSOR Prepared by Tahani Alahmadi

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

Page 7: IMPLICIT AND AN EXPLICIT CURSOR Prepared by Tahani Alahmadi

Controlling Explicit Cursors

Page 8: IMPLICIT AND AN EXPLICIT CURSOR Prepared by Tahani Alahmadi

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.

Page 9: IMPLICIT AND AN EXPLICIT CURSOR Prepared by Tahani Alahmadi

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;

Page 10: IMPLICIT AND AN EXPLICIT CURSOR Prepared by Tahani Alahmadi

Controlling Explicit Cursors

Page 11: IMPLICIT AND AN EXPLICIT CURSOR Prepared by Tahani Alahmadi

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.

Page 12: IMPLICIT AND AN EXPLICIT CURSOR Prepared by Tahani Alahmadi

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.

Page 13: IMPLICIT AND AN EXPLICIT CURSOR Prepared by Tahani Alahmadi

Opening the Cursor

Page 14: IMPLICIT AND AN EXPLICIT CURSOR Prepared by Tahani Alahmadi

Fetching Data from the Cursor

Page 15: IMPLICIT AND AN EXPLICIT CURSOR Prepared by Tahani Alahmadi

Fetching Data from the Cursor

Page 16: IMPLICIT AND AN EXPLICIT CURSOR Prepared by Tahani Alahmadi

Fetching Data from the Cursor

Page 17: IMPLICIT AND AN EXPLICIT CURSOR Prepared by Tahani Alahmadi

Fetching Data from the Cursor

Page 18: IMPLICIT AND AN EXPLICIT CURSOR Prepared by Tahani Alahmadi

Closing the Cursor

Page 19: IMPLICIT AND AN EXPLICIT CURSOR Prepared by Tahani Alahmadi

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.

Page 20: IMPLICIT AND AN EXPLICIT CURSOR Prepared by Tahani Alahmadi

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.

Page 21: IMPLICIT AND AN EXPLICIT CURSOR Prepared by Tahani Alahmadi

Cursors and Records

Process the rows of the active set by fetching values into a PL/SQL RECORD.

Page 22: IMPLICIT AND AN EXPLICIT CURSOR Prepared by Tahani Alahmadi

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.

Page 23: IMPLICIT AND AN EXPLICIT CURSOR Prepared by Tahani Alahmadi

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

Page 24: IMPLICIT AND AN EXPLICIT CURSOR Prepared by Tahani Alahmadi

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.

Page 25: IMPLICIT AND AN EXPLICIT CURSOR Prepared by Tahani Alahmadi

Cursor FOR Loops

Page 26: IMPLICIT AND AN EXPLICIT CURSOR Prepared by Tahani Alahmadi

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> /

Page 27: IMPLICIT AND AN EXPLICIT CURSOR Prepared by Tahani Alahmadi

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.

Page 28: IMPLICIT AND AN EXPLICIT CURSOR Prepared by Tahani Alahmadi

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.

Page 29: IMPLICIT AND AN EXPLICIT CURSOR Prepared by Tahani Alahmadi

Explicit Cursor AttributesObtain status information about a

cursor.

Page 30: IMPLICIT AND AN EXPLICIT CURSOR Prepared by Tahani Alahmadi

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:

Page 31: IMPLICIT AND AN EXPLICIT CURSOR Prepared by Tahani Alahmadi

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.

Page 32: IMPLICIT AND AN EXPLICIT CURSOR Prepared by Tahani Alahmadi

Cursor FOR Loops Using Subqueries

No need to declare the cursor. Example:

Page 33: IMPLICIT AND AN EXPLICIT CURSOR Prepared by Tahani Alahmadi

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.

Page 34: IMPLICIT AND AN EXPLICIT CURSOR Prepared by Tahani Alahmadi

Cursors with Parameters

Page 35: IMPLICIT AND AN EXPLICIT CURSOR Prepared by Tahani Alahmadi

Cursors with Parameters

Page 36: IMPLICIT AND AN EXPLICIT CURSOR Prepared by Tahani Alahmadi

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