This is an
excerpt from the book
Advanced PL/SQL: The Definitive Reference by Boobal Ganesan.
The FETCH_ROWS function acts as a
flag which returns the integer value 1 for each row fetched
from the cursor and 0 if no rows are fetched. This function
when executed, returns the flag value of the current row in
the cursor and moves the control on to the next row and
returns the corresponding flag value during its next
execution respectively.
The prototype for defining the
FETCH_ROWS function is shown below,
DBMS_SQL.FETCH_ROWS(<Cursor_ID>);
In the below example, the first 3
rows from the EMPLOYEES table are parsed and executed. The
cursor now holds 3 records from the EMPLOYEES table and when
the FETCH_ROWS procedure is executed for 3 times, the value
returned would be 1 and if it is executed for the 4th
time, the function returns the value 0 as there is no 4th
row available in the cursor.
1.
SET SERVEROUTPUT ON
200000;
2.
DECLARE
3.
l_i_cursor_id
INTEGER;
4.
l_n_rowcount
NUMBER;
5.
BEGIN
6.
l_i_cursor_id:=dbms_sql.open_cursor;
7.
dbms_sql.parse(l_i_cursor_id,'select
first_name,last_name from employees where rownum<=3',dbms_sql.native);
8.
l_n_rowcount:=dbms_sql.execute(l_i_cursor_id);
9.
dbms_output.put_line('The
FETCH_ROWS value before fetching the 1st row: '||dbms_sql.fetch_rows(l_i_cursor_id));
10.
dbms_output.put_line('The
FETCH_ROWS value before fetching the 2nd row: '||dbms_sql.fetch_rows(l_i_cursor_id));
11.
dbms_output.put_line('The
FETCH_ROWS value before fetching the 3rd row: '||dbms_sql.fetch_rows(l_i_cursor_id));
12.
dbms_output.put_line('The
FETCH_ROWS value before fetching the 4st row: '||dbms_sql.fetch_rows(l_i_cursor_id));
13.
Dbms_sql.close_cursor(l_i_cursor_id);
14.
END;
15.
/
Result:
The FETCH_ROWS value before fetching
the 1st row: 1
The FETCH_ROWS value before fetching
the 2nd row: 1
The FETCH_ROWS value before fetching
the 3rd row: 1
The FETCH_ROWS value before fetching
the 4st row: 0
Line No.
|
Description
|
1
|
This environment variable opens up an output buffer
of size limit of 200000.
|
2
|
Start of the declare section of the block.
|
3
|
A local variable l_i_cursor_id is declared with
INTEGER data type.
|
4
|
A local variable l_n_rowcount is declared with
NUMBER data type.
|
5
|
Start of the execution section of the block.
|
6
|
The function DBMS_SQL.OPEN_CURSOR is executed and
its return value is assigned to the local variable
l_i_cursor_id.
|
7
|
The procedure DBMS_SQL.PARSE is executed with its
first parameter as the local variable l_i_cursor_id,
the second parameter to a dynamic SELECT query
selecting the first name and the last name of the
first 3 employees from the EMPLOYEES table and its
third parameter as the function DBMS_SQL.NATIVE.
|
8
|
The function DBMS_SQL.EXECUTE is executed with its
input parameter as the local variable l_i_cursor_id
and the RETURN value is assigned to the local
variable l_n_rowcount.
|
9-12
|
The function DBMS_SQL.FETCH_ROW is executed four
times with its input parameter as the local variable
l_i_cursor_id and returns the flag stating the
confirmation on the rows retrieved.
|
13
|
The cursor is closed using the procedure
DBMS_SQL.CLOSE_CURSOR with its input parameter as
the local variable l_i_cursor_id.
|
14,15
|
End of the execution section of the block.
|
|
Need to learn to program with PL/SQL?
For complete notes on programming in PL/SQL, we
recommend the book
Advanced PL/SQL: The Definitive Reference by Boobal Ganesan.
This is a complete book on PL/SQL with
everything you need to know to write efficient and
complex PL/SQL code. |
|
|
|
Oracle Training from Don Burleson
The best on site
"Oracle
training classes" are just a phone call away! You can get personalized Oracle training by Donald Burleson, right at your shop!
|
|
|
|
|
Burleson is the American Team
Note:
This Oracle
documentation was created as a support and Oracle training reference for use by our
DBA performance tuning consulting professionals.
Feel free to ask questions on our
Oracle forum.
Verify
experience!
Anyone
considering using the services of an Oracle support expert should
independently investigate their credentials and experience, and not rely on
advertisements and self-proclaimed expertise. All legitimate Oracle experts
publish
their Oracle
qualifications.
Errata?
Oracle technology is changing and we
strive to update our BC Oracle support information. If you find an error
or have a suggestion for improving our content, we would appreciate your
feedback. Just
e-mail:
and include the URL for the page.
Copyright © 1996 - 2020
All rights reserved by
Burleson
Oracle ®
is the registered trademark of Oracle Corporation.
|
|