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 implicit cursor in FOR loop

Oracle PL/SQL tips by Boobal Ganesan

This is an excerpt from the book Advanced PL/SQL: The Definitive Reference by Boobal Ganesan.

An implicit cursor FOR loop has a SELECT statement querying a table or a view instead of lower bound and upper bound values in the case of the traditional FOR loops. The cursor index of the FOR loop acts as a pointer to the result set processed by the associated SELECT statement in the private memory area known as the context area in the PGA. The cursor index points to each row and the period operator link the cursor index and the column alias in the SELECT statement for processing every row-column data for every loop spin.

 

The prototype for defining an implicit cursor FOR loop is shown below,

 

FOR <Loop_index> IN (<Select_statement>)

LOOP

<Executable_statements>;

END LOOP [<Loop_index>];

 

In the below listing, an implicit cursor FOR loop with a SELECT statement is processed for a set of employees. Here, the OPEN stage is started internally once the program control reaches the FOR loop, the FETCH operation is performed inside the loop and the CLOSE stage is performed once the program control exits the loop. Here, the loop’s index name LOOP_EMP acts as the implicit cursor’s name for fetching the record set of the cursor associated SELECT statement.

 

% Note: The implicit cursor FOR loops always returns a Null for the rowcount attribute.

There is no need for an exit statement in any of the FOR loop types as it is internally handled by the cursor index. When the cursor index reads no more rows, the loop exits automatically.

 

BEGIN

  FOR loop_emp IN

  (SELECT *

    FROM

      employees

    WHERE salary>15000)

  LOOP

    dbms_output.put_line('Number of rows processed: '||nvl(to_char(sql%rowcount),'Null'));

  END LOOP loop_emp;

END;

/

 

Result:

 

Number of rows processed: Null

Number of rows processed: Null

Number of rows processed: Null

Number of rows processed: Null

 

In the below snippet, an INTO clause is used in the SELECT query used in the implicit cursor FOR loop. When this block is executed, the cursor variable returns the required value, but the variable in the INTO clause remains null.

 

This is an inconsistent situation where the Oracle documentation has stated that an INTO clause is not allowed inside an implicit cursor FOR loop, but we are able to use one yet with no purpose.

 

Oracle documentation in 12c about the SELECT statement in the implicit cursor FOR loops,

 

SQL SELECT statement (not PL/SQL SELECT INTO statement). For select_statement, PL/SQL declares, opens, fetches from, and closes an implicit cursor. However, because select_statement is not an independent statement, the implicit cursor is internal—you cannot reference it with the nameSQL.

 

DECLARE

  l_vc_name VARCHAR2(100);

BEGIN

  FOR loop_emp IN

  (

    SELECT last_name ||', '||first_name name

    INTO l_vc_name

    FROM employees

    WHERE employee_id=123

  )

  LOOP

    dbms_output.put_line('Cursor Result: '||loop_emp.name);

    dbms_output.put_line('INTO clause Result: '||NVL(l_vc_name,'Null'));

  END LOOP loop_emp;

END;

/

 

Result:

 

Cursor Result: Vollman, Shanta

INTO clause Result: Null

 

 

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