Cursor FOR Loops. 2 home back first prev next last What Will I Learn? List and explain the benefits of using cursor FOR loops Create PL/SQL code to declare.

Slides:



Advertisements
Similar presentations
8 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Using Explicit Cursors.
Advertisements

PL/SQL.
SQL*PLUS, PLSQL and SQLLDR Ali Obaidi. SQL Advantages High level – Builds on relational algebra and calculus – Powerful operations – Enables automatic.
Murali Mani Persistent Stored Modules (Stored Procedures) : PSM.
Persistent State of Package Variables. 2 home back first prev next last What Will I Learn? Identify persistent states of package variables Control the.
Cursors How to step through the returned set of rows.
Chapter 4B: More Advanced PL/SQL Programming
Advanced Package Concepts. 2 home back first prev next last What Will I Learn? Write packages that use the overloading feature Write packages that use.
Creating Packages. 2 home back first prev next last What Will I Learn? Describe the reasons for using a package Describe the two components of a package:
A Guide to Oracle9i1 Advanced SQL And PL/SQL Topics Chapter 9.
Introduction to PL/SQL
Cursors in Pl/SQL Database 1. Practice. Sample Database The schema of the sample database is the following: Drinkers (name, occupation, birthday, salary)
Bordoloi and Bock CURSORS. Bordoloi and Bock CURSOR MANIPULATION To process an SQL statement, ORACLE needs to create an area of memory known as the context.
Cursors in PL/SQL Includes cursor example and continuation of first cursor example Please use speaker notes for additional information!
PL / SQL P rocedural L anguage / S tructured Q uery L anguage Chapter 7 in Lab Reference.
Cursor and Exception Handling By Nidhi Bhatnagar.
Oracle10g Developer: PL/SQL Programming1 Objectives Manipulating data with cursors Managing errors with exception handlers Addressing exception-handling.
Chapter 4 Cursors and Exception Handling Oracle10g Developer:
Benefits of PL/SQL. 2 home back first prev next last What Will I Learn? In this lesson, you will learn to: –List and explain the benefits of PL/SQL –List.
Good Programming Practices. 2 home back first prev next last What Will I Learn? List examples of good programming practices Accurately insert comments.
Revision on Triggers and Cursors. Walk through of exam type question. Question 1. A trigger is required to automatically update the number of rooms available.
Program with PL/SQL. Interacting with the Oracle Server.
Overview · What is PL/SQL · Advantages of PL/SQL · Basic Structure of a PL/SQL Block · Procedure · Function · Anonymous Block · Types of Block · Declaring.
PL/SQL Cursors Session - II. Attributes Attributes %TYPE %ROWTYPE % Found % NotFound % RowCount % IsOPen %TYPE %ROWTYPE % Found % NotFound % RowCount.
L/O/G/O Working with Composite Data Types. Objectives After completing this lesson, you should be able to do the following: –Create user-defined PL/SQL.
Objectives After completing this lesson, you should be able to do the following: Describe each data manipulation language (DML) statement Insert rows.
1 CursorsCursors. 2 SQL Cursor A cursor is a private SQL work area. A cursor is a private SQL work area. There are two types of cursors: There are two.
Trapping Oracle Server Exceptions. 2 home back first prev next last What Will I Learn? Describe and provide an example of an error defined by the Oracle.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
PL/SQL Block Structure DECLARE - Optional Variables, cursors, user-defined exceptions BEGIN - Mandatory SQL Statements PL/SQL Statements EXCEPTIONS - Optional.
Manipulating Data in PL/SQL. 2 home back first prev next last What Will I Learn? Construct and execute PL/SQL statements that manipulate data with DML.
Trigger Oracle PL/SQL. Triggers Associated with a particular table Associated with a particular table Automatically executed when a particular event occurs.
Recognizing PL/SQL Lexical Units. 2 home back first prev next last What Will I Learn? List and define the different types of lexical units available in.
Handling Exceptions. 2 home back first prev next last What Will I Learn? Describe several advantages of including exception handling code in PL/SQL Describe.
Creating DDL and Database Event Triggers. 2 home back first prev next last What Will I Learn? Describe events that cause DDL and database event triggers.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 9 Database Triggers.
Using Oracle-Supplied Packages. 2 home back first prev next last What Will I Learn? Describe two common uses for the DBMS_OUTPUT server-supplied package.
ITEC 224 Database Programming PL/SQL Lab Cursors.
Variables and control statements in PL\SQL Chapter 10.
Chapter 16 Cursors and Exceptions. Chapter Objectives  Determine when an explicit cursor is required  Declare, open, and close an explicit cursor 
Dynamic SQL. 2 home back first prev next last What Will I Learn? Recall the stages through which all SQL statements pass Describe the reasons for using.
Introduction to Explicit Cursors. 2 home back first prev next last What Will I Learn? Distinguish between an implicit and an explicit cursor Describe.
Cursors For viewing and updating. Cursors How to step through the returned set of rows.
Retrieving Data in PL/SQL. 2 home back first prev next last What Will I Learn? In this lesson, you will learn to: –Recognize the SQL statements that can.
implicit and an explicit cursor
Using Functions in SQL Statements. 2 home back first prev next last What Will I Learn? List the advantages of user-defined functions in SQL statements.
Passing Parameters. 2 home back first prev next last What Will I Learn? List the types of parameter modes Create a procedure that passes parameters Identify.
Program with PL/SQL Lesson 3. Interacting with the Oracle Server.
1 Copyright © 2004, Oracle. All rights reserved. PL/SQL Programming Concepts: Review.
7 Copyright © 2004, Oracle. All rights reserved. Using Explicit Cursors.
Program with PL/SQL Lesson 4.
Interacting with the Oracle Server
PL/SQL.
Oracle11g: PL/SQL Programming Chapter 4 Cursors and Exception Handling.
Cursors ITEC 224 Database Programming PL/SQL Lab.
Database Management Systems 2
REF Cursors.
Database Management Systems 2
Database Management Systems 2
Handling Exceptions.
Chapter 2 Handling Data in PL/SQL Blocks Oracle9i Developer:
variables and control statements in PL\SQL
“Manipulating Data” Lecture 6.
Cursors.
Database Management Systems 2
“Manipulating Data” Lecture 6.
1 Manipulating Data. 2 Objectives After completing this lesson, you should be able to do the following:  Describe each data manipulation language (DML)
Chapter 8 Advanced SQL.
Using Subqueries to Solve Queries
Using Subqueries to Solve Queries
Presentation transcript:

Cursor FOR Loops

2 home back first prev next last What Will I Learn? List and explain the benefits of using cursor FOR loops Create PL/SQL code to declare a cursor and manipulate it in a FOR loop Create PL/SQL code containing a cursor FOR loop using a subquery

3 home back first prev next last Why Learn It? You have already learned how to declare and use a simple explicit cursor, using DECLARE, OPEN, FETCH in a loop, testing for %NOTFOUND, and CLOSE statements. Wouldn’t it be easier if we could do all this with just one statement? We can do all of this using a cursor FOR loop.

4 home back first prev next last Cursor FOR Loops A cursor FOR loop processes rows in an explicit cursor. It is a shortcut because the cursor is opened, a row is fetched once for each iteration in the loop, the loop exits when the last row is processed, and the cursor is closed automatically. The loop itself is terminated automatically at the end of the iteration when the last row has been fetched. Syntax:

5 home back first prev next last Cursor FOR Loops In the syntax: –record_name Is the name of the implicitly declared record (as cursor_name%ROWTYPE) –cursor_name Is a PL/SQL identifier for the previously declared cursor

6 home back first prev next last Cursor FOR Loops v_emp_record is the record that is implicitly declared. –You can access the fetched data with this implicit record as shown in the slide. –No variables are declared to hold the fetched data by using the INTO clause. –The code does not have OPEN and CLOSE statements.

7 home back first prev next last Cursor FOR Loops Compare the cursor FOR loop code with the expanded code you learned in the previous lesson. The two forms of the code are logically identical to each other and produce exactly the same results.

8 home back first prev next last A Second Example DECLARE CURSOR dept_cursor IS SELECT department_id, department_name FROM departments ORDER BY department_id; BEGIN FOR v_dept_record IN dept_cursor LOOP DBMS_OUTPUT.PUT_LINE(v_dept_record.department_id || ' ' ||v_dept_record.department_name); END LOOP; END;  v_dept_record has been implicitly declared as dept_cursor%ROWTYPE. How many fields does it contain?

9 home back first prev next last Guidelines for Cursor FOR Loops Do not declare the record that controls the loop because it is declared implicitly. The scope of the implicit record is restricted to the loop, so you cannot reference the record outside the loop. You can access fetched data by record_name.column_name.

10 home back first prev next last Testing Cursor Attributes We can still test cursor attributes such as %ROWCOUNT. This example exits from the loop after five rows have been fetched and processed. The cursor is still closed automatically.

11 home back first prev next last Cursor FOR Loops Using Subqueries We can go one step further. We don’t have to declare the cursor at all! Instead, we can specify the SELECT on which the cursor is based directly in the FOR loop. The advantage of this is that all the cursor definition is contained in a single FOR … statement. This makes later changes to the code much easier and quicker. The next slide shows an example.

12 home back first prev next last Cursor FOR Loops Using Subqueries The SELECT clause in the FOR statement is technically a subquery, so we must enclose it in parentheses.

13 home back first prev next last Cursor FOR Loops Using Subqueries Again, compare these two forms of the code. They are logically identical. But which one would you rather write – especially if you hate typing!

14 home back first prev next last Terminology Key terms used in this lesson include: –Cursor FOR loop

15 home back first prev next last Summary In this lesson, you learned to: –List and explain the benefits of using cursor FOR loops –Create PL/SQL code to declare a cursor and manipulate it in a FOR loop –Create PL/SQL code containing a cursor FOR loop using a subquery

16 home back first prev next last Try It/Solve It The exercises in this lesson cover the following topics: –Listing and explaining the benefits of using cursor FOR loops –Creating PL/SQL code to declare a cursor and manipulate it in a FOR loop –Create PL/SQL code containing a cursor FOR loop using a subquery