Upload
daniela-lutic
View
223
Download
0
Embed Size (px)
Citation preview
8/12/2019 Ora Cursori PL SQL
1/12
PBD Oracle Cursori expliciti-1
Copyright Oracle Corporation, 1998. All rights reserved.
2121
Writing Explicit CursorsWriting Explicit Cursors
21-2 Copyright Oracle Corporation, 1998. All rights reserved.
ObjectivesObjectives
After completing this lesson, you shouldbe able to do the following:
Distinguish between an implicit and an
explicit cursor
Use a PL/SQL record variable
Write a Cursor FOR loop
After completing this lesson, you shouldAfter completing this lesson, you should
be able to do the following:be able to do the following:
Distinguish between an implicit and an
explicit cursor
Use a PL/SQL record variable
Write a Cursor FOR loop
8/12/2019 Ora Cursori PL SQL
2/12
PBD Oracle Cursori expliciti-2
21-3 Copyright Oracle Corporation, 1998. All rights reserved.
About CursorsAbout Cursors
Every SQL statement executed by theOracle Server has an individual cursorassociated with it:
Implicit cursors: Declared for all DMLand PL/SQL SELECT statements.
Explicit cursors: Declared and named
by the programmer.
Every SQL statement executed by theEvery SQL statement executed by the
Oracle Server has an individual cursorOracle Server has an individual cursor
associated with it:associated with it:
Implicit cursors: Declared for all DMLand PL/SQL SELECT statements.
Explicit cursors: Declared and named
by the programmer.
21-4 Copyright Oracle Corporation, 1998. All rights reserved.
Explicit Cursor FunctionsExplicit Cursor FunctionsExplicit Cursor Functions
Result SetResult Set
Current RowCurrent RowCursor
7369 SMITH CLERK
7566 JONES MANAGER
7788 SCOTT ANALYST
7876 ADAMS CLERK
7902 FORD ANALYST
8/12/2019 Ora Cursori PL SQL
3/12
PBD Oracle Cursori expliciti-3
21-5 Copyright Oracle Corporation, 1998. All rights reserved.
Controlling Explicit CursorsControlling Explicit CursorsControlling Explicit Cursors
Create aCreate anamednamed
SQL areaSQL area
DECLAREDECLAREDECLARE
IdentifyIdentifythe activethe active
setset
OPENOPENOPEN
Load theLoad thecurrentcurrent
row intorow into
variablesvariables
FETCHFETCHFETCH
Test forTest forexistingexisting
rowsrows
EMPTY?
Return toReturn toFETCH ifFETCH if
rowsrows
foundfound
NoNo
ReleaseReleasethe activethe active
setset
CLOSECLOSECLOSE
YesYes
21-6 Copyright Oracle Corporation, 1998. All rights reserved.
Controlling Explicit CursorsControlling Explicit CursorsControlling Explicit CursorsOpen the cursor.Open the cursor.
CursorCursor
PointerPointer
Fetch a Row from the cursor.Fetch a Row from the cursor.
CursorCursor
PointerPointer
Continue until empty.Continue until empty.
CursorCursor
PointerPointer
Close the cursor.Close the cursor.
CursorCursor
8/12/2019 Ora Cursori PL SQL
4/12
PBD Oracle Cursori expliciti-4
21-7 Copyright Oracle Corporation, 1998. All rights reserved.
Declaring the CursorDeclaring the CursorDeclaring the CursorSyntax
Do not include the INTO clause in thecursor declaration.
If processing rows in a specificsequence is required use the ORDERBY clause in the query.
SyntaxSyntax
Do not include the INTO clause in thecursor declaration.
If processing rows in a specificsequence is required use the ORDERBY clause in the query.
CURSOR cursor_name IS
select_statement;
CURSOR cursor_name IS
select_statement;
21-8 Copyright Oracle Corporation, 1998. All rights reserved.
Declaring the CursorDeclaring the CursorDeclaring the Cursor
ExampleExampleExample
DECLARECURSOR c1 ISSELECT empno, enameFROM emp;
CURSOR c2 ISSELECT *FROM deptWHERE deptno = 10;
BEGIN...
DECLARECURSOR c1 ISSELECT empno, enameFROM emp;
CURSOR c2 ISSELECT *FROM deptWHERE deptno = 10;
BEGIN...
8/12/2019 Ora Cursori PL SQL
5/12
PBD Oracle Cursori expliciti-5
21-9 Copyright Oracle Corporation, 1998. All rights reserved.
Opening the CursorOpening the CursorOpening the CursorSyntax
Open the cursor to execute the queryand identify the active set.
If the query returns no rows, noexception is raised.
Use cursor attributes to test theoutcome after a fetch.
SyntaxSyntax
Open the cursor to execute the queryand identify the active set.
If the query returns no rows, noexception is raised.
Use cursor attributes to test theoutcome after a fetch.
OPEN cursor_name;OPEN cursor_name;
21-10 Copyright Oracle Corporation, 1998. All rights reserved.
Fetching Data from the CursorFetching Data from the CursorFetching Data from the Cursor
Syntax
Retrieve the current row values intooutput variables.
Include the same number of variables.
Match each variable to correspond tothe columns positionally.
Test to see if the cursor contains rows.
SyntaxSyntax
Retrieve the current row values intooutput variables.
Include the same number of variables.
Match each variable to correspond tothe columns positionally.
Test to see if the cursor contains rows.
FETCHcursor_name INTO [variable1, variable2, ...]
| record_name];
FETCHcursor_name INTO [variable1, variable2, ...]
| record_name];
8/12/2019 Ora Cursori PL SQL
6/12
PBD Oracle Cursori expliciti-6
21-11 Copyright Oracle Corporation, 1998. All rights reserved.
Fetching Data from the CursorFetching Data from the CursorFetching Data from the Cursor
ExamplesExamplesExamples
FETCH c1 INTO v_empno, v_ename;FETCH c1 INTO v_empno, v_ename;
...OPEN defined_cursor;LOOPFETCH defined_cursorINTO defined_variablesEXIT WHEN ...;
...-- Process the retrieved data
...END;
...OPEN defined_cursor;LOOPFETCH defined_cursorINTO defined_variablesEXIT WHEN ...;
...-- Process the retrieved data
...END;
21-12 Copyright Oracle Corporation, 1998. All rights reserved.
Closing the CursorClosing the CursorClosing the Cursor
Syntax
Close the cursor after completing theprocessing of the rows.
Reopen the cursor, if required.
Do not attempt to fetch data from acursor once it has been closed.
SyntaxSyntax
Close the cursor after completing theprocessing of the rows.
Reopen the cursor, if required.
Do not attempt to fetch data from acursor once it has been closed.
CLOSE cursor_name;CLOSE cursor_name;
8/12/2019 Ora Cursori PL SQL
7/12
PBD Oracle Cursori expliciti-7
21-13 Copyright Oracle Corporation, 1998. All rights reserved.
Explicit Cursor AttributesExplicit Cursor AttributesExplicit Cursor AttributesObtain status information about a cursor.Obtain status information about a cursor.Obtain status information about a cursor.
Attribute Type Description
%ISOPEN Boolean Evaluates to TRUE if the cursor
is open
%NOTFOUND Boolean Evaluates to TRUE if the most
recent fetch does not return a row
%FOUND Boolean Evaluates to TRUE if the most
recent fetch returns a row;
complement of %NOTFOUND
%ROWCOUNT Number Evaluates to the total number of
rows returned so far
21-14 Copyright Oracle Corporation, 1998. All rights reserved.
Controlling Multiple FetchesControlling Multiple Fetches
Process several rows from an explicitcursor using a loop.
Fetch a row with each iteration.
Use the %NOTFOUND attribute to writea test for an unsuccessful fetch.
Use explicit cursor attributes to test thesuccess of each fetch.
Process several rows from an explicitcursor using a loop.
Fetch a row with each iteration.
Use the %NOTFOUND attribute to writea test for an unsuccessful fetch.
Use explicit cursor attributes to test thesuccess of each fetch.
8/12/2019 Ora Cursori PL SQL
8/12
PBD Oracle Cursori expliciti-8
21-15 Copyright Oracle Corporation, 1998. All rights reserved.
The %ISOPEN AttributeThe %ISOPEN AttributeThe %ISOPEN Attribute Fetch rows only when the cursor is
open.
Use the %ISOPEN cursor attributebefore performing a fetch to testwhether the cursor is open.
Example
Fetch rows only when the cursor isopen.
Use the %ISOPEN cursor attributebefore performing a fetch to testwhether the cursor is open.
ExampleExample
IF NOT c1%ISOPEN THEN
OPEN c1;END IF;
LOOP
FETCH c1...
IF NOT c1%ISOPEN THEN
OPEN c1;END IF;
LOOP
FETCH c1...
21-16 Copyright Oracle Corporation, 1998. All rights reserved.
The %NOTFOUND and
%ROWCOUNT Attributes
The %NOTFOUND and
%ROWCOUNT Attributes
Use the %ROWCOUNT cursor attributeto retrieve an exact number of rows.
Use the %NOTFOUND cursor attributeto determine when to exit the loop.
Use the %ROWCOUNT cursor attributeto retrieve an exact number of rows.
Use the %NOTFOUND cursor attributeto determine when to exit the loop.
8/12/2019 Ora Cursori PL SQL
9/12
PBD Oracle Cursori expliciti-9
21-17 Copyright Oracle Corporation, 1998. All rights reserved.
Cursors and RecordsCursors and RecordsCursors and RecordsProcess the rows of the active setconveniently by fetching values into aPL/SQL RECORD.
Example
Process the rows of the active setProcess the rows of the active set
conveniently by fetching values into aconveniently by fetching values into a
PL/SQL RECORD.PL/SQL RECORD.
ExampleExample
...CURSOR c1 ISSELECT empno, enameFROM emp;
emp_record c1%ROWTYPE;
BEGINOPEN c1;
. . .FETCH c1 INTO emp_record;
...CURSOR c1 ISSELECT empno, enameFROM emp;
emp_record c1%ROWTYPE;
BEGINOPEN c1;
. . .FETCH c1 INTO emp_record;
21-18 Copyright Oracle Corporation, 1998. All rights reserved.
Syntax
Shortcut to process explicit cursors.
Implicit open, fetch, and close occur.
Do not declare the record; it isimplicitly declared.
SyntaxSyntax
Shortcut to process explicit cursors.
Implicit open, fetch, and close occur.
Do not declare the record; it isimplicitly declared.
Cursor FOR LoopsCursor FOR LoopsCursor FOR Loops
FOR record_name IN cursor_name LOOP
statement1;
statement2;
. . .END LOOP;
FOR record_name IN cursor_name LOOP
statement1;
statement2;
. . .END LOOP;
8/12/2019 Ora Cursori PL SQL
10/12
PBD Oracle Cursori expliciti-10
21-19 Copyright Oracle Corporation, 1998. All rights reserved.
Cursor FOR LoopsCursor FOR LoopsCursor FOR LoopsRetrieve employees one by one until thereare no more left.
Example
Retrieve employees one by one until thereRetrieve employees one by one until thereare no more left.are no more left.
ExampleExample
DECLARECURSOR c1 ISSELECT empno, enameFROM emp;
BEGINFOR emp_record IN c1 LOOP
-- implicit open and implicit fetch occurIF emp_record.empno = 7839 THEN...
END LOOP; -- implicit close occursEND;
DECLARECURSOR c1 ISSELECT empno, enameFROM emp;
BEGINFOR emp_record IN c1 LOOP
-- implicit open and implicit fetch occurIF emp_record.empno = 7839 THEN...
END LOOP; -- implicit close occursEND;
21-20 Copyright Oracle Corporation, 1998. All rights reserved.
Cursor FOR Loops Using
Subqueries
Cursor FOR Loops Using
Subqueries
No Need to declare the cursor.
Example
No Need to declare the cursor.No Need to declare the cursor.
ExampleExample
BEGINFOR emp_record IN ( SELECT empno, enameFROM emp) LOOP
-- implicit open and implicit fetch occurIF emp_record.empno = 7839 THEN...
END LOOP; -- implicit close occursEND;
BEGIN
FOR emp_record IN ( SELECT empno, enameFROM emp) LOOP
-- implicit open and implicit fetch occurIF emp_record.empno = 7839 THEN...
END LOOP; -- implicit close occursEND;
8/12/2019 Ora Cursori PL SQL
11/12
PBD Oracle Cursori expliciti-11
21-21 Copyright Oracle Corporation, 1998. All rights reserved.
SummarySummary Cursor types:
Implicit cursors: Used for all DMLstatements and single-row queries.
Explicit cursors: Used for queries ofzero, one, or more rows.
Manipulate explicit cursors.
Evaluate the cursor status by usingcursor attributes.
Use cursor FOR loops.
Cursor types:
Implicit cursors: Used for all DMLstatements and single-row queries.
Explicit cursors: Used for queries ofzero, one, or more rows.
Manipulate explicit cursors.
Evaluate the cursor status by usingcursor attributes.
Use cursor FOR loops.
21-22 Copyright Oracle Corporation, 1998. All rights reserved.
Practice OverviewPractice Overview
Declaring and using explicit cursors toquery rows of a table
Using a cursor FOR loop
Applying cursor attributes to test thecursor status
Declaring and using explicit cursors toquery rows of a table
Using a cursor FOR loop
Applying cursor attributes to test thecursor status
8/12/2019 Ora Cursori PL SQL
12/12
PBD Oracle Cursori expliciti-12
21-23 Copyright Oracle Corporation, 1998. All rights reserved.
21-24 Copyright Oracle Corporation, 1998. All rights reserved.