Call now: 252-767-6166  
Oracle Training Oracle Support Development Oracle Apps

 
 Home
 E-mail Us
 Oracle Articles
New Oracle Articles


 Oracle Training
 Oracle Tips

 Oracle Forum
 Class Catalog


 Remote DBA
 Oracle Tuning
 Emergency 911
 RAC Support
 Apps Support
 Analysis
 Design
 Implementation
 Oracle Support


 SQL Tuning
 Security

 Oracle UNIX
 Oracle Linux
 Monitoring
 Remote s
upport
 Remote plans
 Remote
services
 Application Server

 Applications
 Oracle Forms
 Oracle Portal
 App Upgrades
 SQL Server
 Oracle Concepts
 Software Support

 Remote S
upport  
 Development  

 Implementation


 Consulting Staff
 Consulting Prices
 Help Wanted!

 


 Oracle Posters
 Oracle Books

 Oracle Scripts
 Ion
 Excel-DB  

Don Burleson Blog 


 

 

 


 

 

 
 

PL/SQL dbms_sql fetch_rows tips

Oracle PL/SQL tips by Boobal Ganesan

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

Script Explanation

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!

Oracle training
 
 



 

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.


                    









Burleson Consulting

The Oracle of Database Support

Oracle Performance Tuning

Remote DBA Services


 

Copyright © 1996 -  2020

All rights reserved by Burleson

Oracle ® is the registered trademark of Oracle Corporation.

 

 

��  
 
 
Oracle Training at Sea
 
 
 
 
oracle dba poster
 

 
Follow us on Twitter 
 
Oracle performance tuning software 
 
Oracle Linux poster