Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 34 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
34
Dung lượng
232,64 KB
Nội dung
ProcessingQueriesby Using
Explicit Cursors
24
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder24Ć2
Processing QueriesbyUsingExplicitCursors 24Ć3
Objectives
You may need to use a multiple row SELECT statement within PL/SQL to
process many rows. To accomplish this, you declare and control explicit cursors,
which are used in loops, including the cursor FOR loop.
At the end of this lesson, you should be able to
D Explain the difference between implicit and explicit cursors.
D Declare and use explicitcursors to fetch rows from the database.
D Create an explicit cursor containing parameters.
D Write cursor FOR loops.
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder24Ć4
Processing QueriesbyUsingExplicitCursors 24Ć5
Overview
The Oracle7 Server uses work areas called “private SQL areas” to execute SQL
statements and store processing information. PL/SQL cursors let you name a private
SQL area and access its stored information. The cursor directs all phases of
processing.
Cursor Type
Description
Implicit Declared by PL/SQL implicitly for all DML and PL/SQL
SELECT statements.
Explicit Declared and named by the programmer and manipulated
through specific statements within the block’s executable
actions.
Recall that the SELECT statement in PL/SQL must only return a single row. PL/SQL
actually attempts to fetch two rows from an implicit cursor: one to satisfy the query,
and a second to see if further rows were returned. One method to eliminate this extra
fetch is to use an explicit cursor.
Explicit Cursor Functions
D Can process beyond the first row returned by the query, row by row.
D Keep track of which row is currently being processed.
D Allow the programmer to manually control them in the PL/SQL block.
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder24Ć6
Processing QueriesbyUsingExplicitCursors 24Ć7
Controlling Explicit Cursors
Now that you have a conceptual understanding of cursors, review the steps to use
them. The syntax for each step follows on the next pages.
Controlling ExplicitCursorsUsing Four Commands
1. Declare the cursor.
Declare the cursor by naming it and defining the structure of the query to be
performed within it.
2. Open the cursor.
The OPEN statement executes the query and binds any variables that are
referenced. Rows identified by the query are called the active set and are now
available for fetching.
3. Fetch data from the cursor.
The FETCH statement loads the current row from the cursor into variables. Each
fetch causes the cursor to move its pointer to the next row in the active set.
Therefore, each fetch will access a different row returned by the query.
In the flow diagram on the left page, each fetch tests the cursor for any existing
rows. If rows are found, it loads the current row into variables, else it closes the
cursor.
4. Close the cursor.
The CLOSE statement releases the active set of rows. It is now possible to reopen
the cursor to establish a fresh active set.
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder24Ć8
Processing QueriesbyUsingExplicitCursors 24Ć9
Controlling ExplicitCursors continued
Declaring the Cursor
Use the CURSOR statement to declare an explicit cursor. You can define parameters
to allow substitution of values into the query when the cursor is opened. You can also
reference variables within the query, but you must declare them before the cursor
statement.
Syntax
DECLARE
CURSOR cursor_name IS
select_statement;
where: cursor_name is a PL/SQL identifier.
select_statement is a SELECT statement without an INTO
clause.
Note: Do not include the INTO clause within the cursor declaration because it
appears later within the FETCH statement.
1
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder24Ć10
[...]... Release 2.3 Using Cursor Attributes” section ProcessingQueriesbyUsingExplicitCursors 24Ć17 24Ć18 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Explicit Cursor Attributes continued You can only fetch rows when the cursor is open Determine whether the cursor is open using the %ISOPEN cursor attribute, if necessary Fetch rows in a loop Determine when to exit the loop byusing cursor... TO_CHAR(v_order_total,’$999,999,999.99’)); END LOOP; CLOSE item_cursor; END ord_process; Note: If using %ROWCOUNT, add a test for no rows in the cursor byusing the %NOTFOUND attribute because the rowcount is not incremented if the fetch does not retrieve any rows ProcessingQueriesbyUsingExplicitCursors 24Ć19 24Ć20 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Cursors and Records We have already seen that you can define... your knowledge of cursors to process a number of rows from a table and populate another table with the results using a cursor FOR loop Practice Contents D Declaring and using a cursor to query rows of a table D Using a cursor FOR loop D Applying cursor attributes to test the cursor status ProcessingQueriesbyUsingExplicitCursors 24Ć31 24Ć32 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder... test the cursor’s status after a fetch ProcessingQueriesbyUsingExplicitCursors 24Ć11 24Ć12 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Controlling ExplicitCursors 3 continued Fetching Data from the Cursor Use the FETCH statement to retrieve the current row values into output variables After the fetch, you can manipulate the variables by further statements Syntax FETCH cursor_name... values, that is, there are now rows left to process in the active set and no error is recorded ProcessingQueriesbyUsingExplicitCursors 24Ć13 24Ć14 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Controlling ExplicitCursors 4 continued Closing the Cursor Close the cursor after completing the processing of the SELECT statement This step allows the cursor to be reopened, if required Therefore,... SQL and PL/SQL Using Procedure Builder Summary Cursor Types D Implicit cursors are used for all DML statements and single row queries D Explicitcursors are used for queries of zero, one, or more rows Explicit Cursor Commands D Declare the cursor D Open the cursor D Fetch data from the cursor D Close the cursor Cursor Attributes Evaluate the status of the cursor byusing cursor attributes Cursors with... been closed or the INVALID_CURSOR exception will be raised ProcessingQueriesbyUsingExplicitCursors 24Ć15 24Ć16 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Explicit Cursor Attributes As with implicit cursors, there are four attributes for obtaining status information about a cursor When used, the attribute name is preceded by the cursor identifier Cursor Attribute Description %ISOPEN... latest row processed by the FETCH statement When you use this clause, the cursor you reference must exist and must contain the FOR UPDATE clause in the cursor query, otherwise you will obtain an error This clause allows you to apply updates and deletes to the currently addressed row without the need to explicitly reference the ROWID pseudo-column ProcessingQueriesbyUsingExplicitCursors 24Ć27 24Ć28... columns in an explicit cursor This is convenient for processing the rows of the active set since you can simply fetch into the record Therefore, the values of the row are loaded directly into the corresponding fields of the record In the example, you can select the ROWID pseudo-column, and it will have a corresponding field within the EMP_RECORD PL/SQL record ProcessingQueriesbyUsingExplicit Cursors. .. parameters to open an explicit cursor several times in a block, returning a different active set on each occasion Cursor FOR Loops Use cursor FOR loops as a shortcut to open the cursor, fetch rows once for each loop iteration, and automatically close the cursor after all rows are processed ProcessingQueriesbyUsingExplicitCursors 24Ć29 24Ć30 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder . Processing Queries by Using
Explicit Cursors
24
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder24Ć2
Processing Queries by Using Explicit. to Oracle: SQL and PL/SQL Using Procedure Builder24Ć6
Processing Queries by Using Explicit Cursors 24Ć7
Controlling Explicit Cursors
Now that you have