implicit and an explicit cursor

Slides:



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

PL/SQL.
AN INTRODUCTION TO PL/SQL Mehdi Azarmi 1. Introduction PL/SQL is Oracle's procedural language extension to SQL, the non-procedural relational database.
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.
Cursors How to step through the returned set of rows.
Chapter 4B: More Advanced PL/SQL Programming
1 PL/SQL programming Procedures and Cursors Lecture 1 Akhtar Ali.
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.
Distributed Database Applications COSC 5050 Week Three.
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:
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.
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.
CIS4368: Advanced DatabaseSlide # 1 PL/SQL Dr. Peeter KirsSpring, 2003 PL/SQL.
8 1 Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
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.
Chapter 15 Introduction to PL/SQL. Chapter Objectives  Explain the benefits of using PL/SQL blocks versus several SQL statements  Identify the sections.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 3 Handling Data in PL/SQL Blocks.
Database Management COP4540, SCS, FIU Oracle PL/SQL (Ch 10.5)
School of Computing and Management Sciences © Sheffield Hallam University SQL is non-procedural –designed to be relatively approachable to non- programmers.
Using SQL in PL/SQL ITEC 224 Database Programming.
Chapter Sixteen Cursors Objective: – Introduction to cursors – Use of cursors in a database record – Implicit & explicit cursors – Cursors & loops – Cursors.
PL/SQLPL/SQL Oracle11g: PL/SQL Programming Chapter 4 Cursors and Exception Handling.
ITEC 224 Database Programming PL/SQL Lab Cursors.
Chapter 16 Cursors and Exceptions. Chapter Objectives  Determine when an explicit cursor is required  Declare, open, and close an explicit cursor 
Introduction to Explicit Cursors. 2 home back first prev next last What Will I Learn? Distinguish between an implicit and an explicit cursor Describe.
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.
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.
Oracle11g: PL/SQL Programming Chapter 3 Handling Data in PL/SQL Blocks.
PL/SQL. What is PL/SQL  Procedural Language – SQL  An extension to SQL with design features of programming languages (procedural and object oriented)
Handling Exceptions. Objectives What is exception Types of exceptions How to handle exceptions Trapping pre defined oracle errors.
Program with PL/SQL Lesson 3. Interacting with the Oracle Server.
Kingdom of Saudi Arabia Ministry of Higher Education Al-Imam Muhammad Ibn Saud Islamic University College of Computer and Information Sciences Overview.
1 Copyright © 2004, Oracle. All rights reserved. PL/SQL Programming Concepts: Review.
7 Copyright © 2004, Oracle. All rights reserved. Using Explicit Cursors.
PL/SQL CURSORS.
CS322: Database Systems PL/ SQL PL/SQL by Ivan Bayross.
Program with PL/SQL Lesson 4.
Interacting with the Oracle Server
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.
Handling Exceptions.
Database Management Systems 2
Using Subqueries to Solve Queries
Interacting with the Oracle Server
Database Management Systems 2
Database Management Systems 2
Handling Exceptions.
Handling Exceptions.
Chapter 2 Handling Data in PL/SQL Blocks Oracle9i Developer:
Cursors.
Using Subqueries to Solve Queries
Chapter 8 Advanced SQL.
Using Subqueries to Solve Queries
Handling Data in PL/SQL Blocks
Using Subqueries to Solve Queries
Presentation transcript:

implicit and an explicit cursor Prepared by Tahani Alahmadi

Objectives After completing this lecture, you should be able to do the following: • Distinguish between an implicit and an explicit cursor • Discuss when and why to use an explicit cursor • Declare and control explicit cursors • Use simple loop and cursor FOR loop to fetch data • Declare and use cursors with parameters • Lock rows using the FOR UPDATE clause • Reference the current row with the WHERE CURRENT clause

About Cursors Every SQL statement executed by the Oracle Server has an individual cursor associated with it: • Implicit cursors: Declared and managed by PL/SQL for all DML and PL/SQL SELECT statements • Explicit cursors: Declared and managed by the programmer

Explicit Cursor Operations

Explicit Cursor Operations You declare explicit cursors in PL/SQL when you have a SELECT statement returning multiple rows. You can process each row returned by the SELECT statement. The set of rows returned by a multiple-row query is called the active set. Its size is the number of rows that meet your search criteria. The diagram in the slide before shows how an explicit cursor “points” to the current row in the active set. This allows your program to process the rows one at a time.

Explicit Cursor functions Can process beyond the first row returned by the query, row by row Keep track of which row is currently being processed Allow the programmer to manually control explicit cursors in the PL/SQL block

Controlling Explicit Cursors

How to use Explicit Cursor? There are four steps in using an Explicit Cursor.  DECLARE the cursor in the declaration section. OPEN the cursor in the Execution Section. FETCH the data from cursor into PL/SQL variables or records in the Execution Section. In the flow diagram shown in the slide before, after each fetch you test the cursor for any existing row. If there are no more rows to process, then you must close the cursor. CLOSE the cursor in the Execution Section before end the PL/SQL Block. The CLOSE statement releases the active set of rows.

How to access an Explicit Cursor? These are the three steps in accessing the cursor. 1) Open the cursor. General Syntax to open a cursor is: OPEN cursor_name; 2) Fetch the records in the cursor one at a time. General Syntax to fetch records from a cursor is: FETCH cursor_name INTO record_name;OR FETCH cursor_name INTO variable_list; 3) Close the cursor. General Syntax to close a cursor is: CLOSE cursor_name;

Controlling Explicit Cursors

Controlling Explicit Cursors 1. The OPEN statement executes the query associated with the cursor, identifies the active set, and positions the cursor to the first row. 2. The FETCH statement retrieves the current row and advances the cursor to the next row until either there are no more rows or until a specified condition is met. 3. The CLOSE statement releases the cursor.

Declaring the Cursor * SELECT statement without an INTO clause * If processing rows in a specific sequence is required, use the ORDER BY clause in the query.

Opening the Cursor

Fetching Data from the Cursor

Fetching Data from the Cursor

Fetching Data from the Cursor

Fetching Data from the Cursor

Closing the Cursor

Example The %ROWTYPE attribute provides a record type that represents a row in a database table. The record can store an entire row of data selected from the table or fetched from a cursor or cursor variable. Variables declared using %ROWTYPE are treated like those declared using a datatype name. You can use the %ROWTYPE attribute in variable declarations as a datatype specifier. Fields in a record and corresponding columns in a row have the same names and datatypes. 1> DECLARE 2> emp_rec emp_tbl%rowtype; 3> CURSOR emp_cur IS 4> SELECT * 5> FROM emp_tbl 6> WHERE salary > 10; 7> BEGIN 8> OPEN emp_cur; 9> FETCH emp_cur INTO emp_rec; 10> dbms_output.put_line (emp_rec.first_name || ' ' || emp_rec.last_name); 11> CLOSE emp_cur; 12> END;

Example In the above example, first we are creating a record ‘emp_rec’ of the same structure as of table ‘emp_tbl’ in line no 2. Second, we are declaring a cursor ‘emp_cur’ from a select query in line no 3 - 6. Third, we are opening the cursor in the execution section in line no 8. Fourth, we are fetching the cursor to the record in line no 9. Fifth, we are displaying the first_name and last_name of the employee in the record emp_rec in line no 10. Sixth, we are closing the cursor in line no 11.

Cursors and Records Process the rows of the active set by fetching values into a PL/SQL RECORD.

Cursor FOR Loops • The cursor FOR loop is a shortcut to process explicit cursors. • Implicit open, fetch, exit, and close occur. • The record is implicitly declared.

Cursor FOR Loops You have learned to fetch data from cursors by using simple loops. You will now learn to use a cursor FOR loop. 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 where the last row is fetched. In the syntax: record_name Is the name of the implicitly declared record cursor_name Is a PL/SQL identifier for the previously declared cursor

Cursor FOR Loops Guidelines Do not declare the record that controls the loop because it is declared implicitly. Test the cursor attributes during the loop, if required. Supply the parameters for a cursor, if required, in parentheses following the cursor name in the FOR statement.

Cursor FOR Loops

use for loops in cursors 1> DECLARE 2> CURSOR emp_cur IS 3> SELECT first_name, last_name, salary FROM emp_tbl; 4> 5> BEGIN 6> FOR emp_rec in emp_cur 7> LOOP 8> dbms_output.put_line(emp_cur.first_name || ' ' ||emp_cur.last_name 9> || ' ' ||emp_cur.salary); 10> END LOOP; 11>END; 12> /

use for loops in cursors when the FOR loop is processed a record ‘emp_rec’of structure ‘emp_cur’ gets created, the cursor is opened, the rows are fetched to the record ‘emp_rec’ and the cursor is closed after the last row is processed. By using FOR Loop in your program, you can reduce the number of lines in the program.

Explicit Cursor Attributes What are Explicit Cursor Attributes? Oracle provides some attributes known as Explicit Cursor Attributes to control the data processing while using cursors. We use these attributes to avoid errors while accessing cursors through OPEN, FETCH and CLOSE Statements. Note: You cannot reference cursor attributes directly in a SQL statement. When does an error occur while accessing an explicit cursor? a) When we try to open a cursor which is not closed in the previous operation. b) When we try to fetch a cursor after the last operation.

Explicit Cursor Attributes Obtain status information about a cursor.

The %ISOPEN Attribute • Fetch rows only when the cursor is open. • Use the %ISOPEN cursor attribute before performing a fetch to test whether the cursor is open. Example:

Example of %ROWCOUNT and %NOTFOUND This example in retrieves the first ten employees one by one. It shows how %ROWCOUNT and %NOTFOUND attributes can be used for exit conditions in a loop.

Cursor FOR Loops Using Subqueries No need to declare the cursor. Example:

Cursor FOR Loops Using Subqueries Observe that there is no declarative section in this PL/SQL block. The difference between the cursor FOR loops using subqueries and the cursor FOR loop lies in the cursor declaration. If you are writing cursor FOR loops using subqueries, you need not declare the cursor in the declarative section. You have to provide the SELECT statement that determines the active set in the loop itself. Note: You cannot reference explicit cursor attributes if you use a subquery in a cursor FOR loop because you cannot give the cursor an explicit name.

Cursors with Parameters

Cursors with Parameters

Summary In this lecture, you should have learned how to: • Distinguish cursor types: – Implicit cursors: Used for all DML statements and single- row queries – Explicit cursors: Used for queries of zero, one, or more rows • Create and handle explicit cursors • Use simple loops and cursor FOR loops to handle multiple rows in the cursors • Evaluate the cursor status by using the cursor attributes http://docs.oracle.com/cd/B19306_01/appdev.102/b142 61/sqloperations.htm