RETRIEVE A NO. OF ROWS ¦ Declare a cursor ¦ Open the cursor ¦ Fetch rows of data ¦ Stop fetching rows ¦ Close the cursor.

Slides:



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

SQL*PLUS, PLSQL and SQLLDR Ali Obaidi. SQL Advantages High level – Builds on relational algebra and calculus – Powerful operations – Enables automatic.
Embedded SQL (Cont.) Pertemuan 10 Matakuliah: T0413/Current Popular IT II Tahun: 2007.
ORACLE TRANSACTIONS A transaction begins with the first executable SQL statement after a commit, rollback or connection made to the Oracle engine. All.
Embedded SQL John Ortiz. Lecture 15Embedded SQL2 Why Isn’t Interactive SQL Enough?  How to do this using interactive SQL?  Print a well-formatted transcript.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13 Introduction to SQL Programming Techniques.
Chapter 4B: More Advanced PL/SQL Programming
A Guide to SQL, Seventh Edition. Objectives Embed SQL commands in PL/SQL programs Retrieve single rows using embedded SQL Update a table using embedded.
SQL/Server Stephen Cunningham Thema Davis. Problem Domain Designed for retrieval and management of data Defines the structures and operations of a data.
1 PL/SQL programming Procedures and Cursors Lecture 1 Akhtar Ali.
DB2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB01/003 Version No:2.0a Session Plan SPUFI Hands On Introduction to Embedded SQL DCLGEN.
Jump-Start Embedded SQL into RPG Presented by: Robert Arce.
Advanced Databases Advanced PL/SQL Programming: Procedure, Function and Package.
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.
Overview of JDBC and Pro*C 1 Overview of JDBC,Pro*C and Oracle connectivity on Omega CSE 5330 – Database Systems.
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:
1 No class Thurs. Oct. 2 E-day Exam 1 Thurs. Oct. 9 HW3 Oct. 2 to –Available Oct 2 only until 2 pm.
Dinamic SQL & Cursor. Why Dinamic SQL ? Sometimes there is a need to dynamically create a SQL statement on the fly and then run that command. This can.
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.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
A Free sample background from © 2006 By Default! R.SARAVANA KUMAR S.NAVEEN Relational Database Model.
Converting To Anthony Tichonoff Florida Hospital MIS January 2007 With Ease Multi-Row Fetch.
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.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Cursors These slides are licensed under.
Procedure Function Trigger. create table employee ( id number, name varchar2(100), deptno number, salary float, primary key(id) ) create table deptincrease.
PL/SQL Cursors Session - II. Attributes Attributes %TYPE %ROWTYPE % Found % NotFound % RowCount % IsOPen %TYPE %ROWTYPE % Found % NotFound % RowCount.
PL SQL Block Structures. What is PL SQL A good way to get acquainted with PL/SQL is to look at a sample program. PL/SQL combines the data manipulating.
Advanced SQL: Cursors & Stored Procedures
ICS 321 Fall 2009 DBMS Application Programming Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 10/06/20091Lipyeow.
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.
ICS 321 Fall 2010 SQL in a Server Environment (i) Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 11/1/20101Lipyeow.
Programmatic SQL Shaista Khan CS 157B. Topic Embedded SQL statements in high-level programming languages.
CIS4368: Advanced DatabaseSlide # 1 PL/SQL Dr. Peeter KirsSpring, 2003 PL/SQL.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 3 Handling Data in PL/SQL Blocks.
Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
DATA RETRIEVAL WITH SQL Goal: To issue a database query using the SELECT command.
PL/SQLPL/SQL Oracle11g: PL/SQL Programming Chapter 4 Cursors and Exception Handling.
ITEC 224 Database Programming PL/SQL Lab Cursors.
Oracle10g Developer: PL/SQL Programming1 Objectives SQL queries within PL/SQL Host or bind variables The %TYPE attribute Include queries and control structures.
Introduction to Explicit Cursors. 2 home back first prev next last What Will I Learn? Distinguish between an implicit and an explicit cursor Describe.
Chapter 8 Embedded SQL.
implicit and an explicit cursor
In this module, we will design a program to work with the PROJECT table, learning to insert a new project into the table, to delete one or more projects.
SCU Fall 2002JoAnne Holliday10–1 Schedule Today u Triggers, Procedures, PL/SQL. u Read Sections , 8.1, 8.5. Next u Transaction concepts, security.
Lu Wei1 Outline Introduction Basic SQL Setting Up and Using PostgreSQL Advanced SQL Embeded SQL.
Program with PL/SQL Lesson 3. Interacting with the Oracle Server.
Lab 2 Writing PL/SQL Blocks CISB514 Advanced Database Systems.
1 Section 10 - Embedded SQL u Many computer languages allow you to embed SQL statements within the code (e.g. COBOL, PowerBuilder, C++, PL/SQL, etc.) u.
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe.
SCU Fall 2002JoAnne Holliday10–1 Schedule Today u Embedded SQL. u Read Sections 8.1, 8.5. Next u Transaction concepts, security u Read Sections 8.6 – 8.7.
7 Copyright © 2004, Oracle. All rights reserved. Using Explicit Cursors.
Introduction to SQL Programming Techniques
Introduction to Database Systems, CS420
A Guide to SQL, Seventh Edition
Program with PL/SQL Lesson 4.
Oracle11g: PL/SQL Programming Chapter 4 Cursors and Exception Handling.
Views, Stored Procedures, Functions, and Triggers
Handling Exceptions.
Interacting with the Oracle Server
Database Management Systems 2
Database Management Systems 2
CPSC-310 Database Systems
Cursors.
Unit I-2.
SQL Subquery.
Information Management
Dynamic SQL The dynamic SQL component of SQL allows programs to construct and submit SQL queries at run time. In contrast, embedded SQL statements must.
Presentation transcript:

RETRIEVE A NO. OF ROWS ¦ Declare a cursor ¦ Open the cursor ¦ Fetch rows of data ¦ Stop fetching rows ¦ Close the cursor

Declaring (defining) a cursor is done in the data division of your program. DATA DIVISION. WORKING-STORAGE SECTION. * * * * * * * * * * * * * * * * * * INCLUDE DECLEGENED STRUCTURES * * * * * * * * * * * * * * * * * * EXEC SQL INCLUDE SQLCA END-EXEC. **** SQL CURSOR DECLARATIONS **** EXEC SQL DECLARE CR1 CURSOR FOR SELECT PROJNO,PROJNAME, DEPTNO,LEADER FROM PROJECT WHERE DEPTNO = :DEPT-NO END-EXEC. This statement declares a cursor which will select the rows from the PROJECT table which have a department number equal to the host variable :DEPT-NO.

Open the cursor Opening the cursor is part of the PROCEDURE DIVISION of a program. This actually causes the SELECT which was coded in the cursor declaration to be executed.

DATA DIVISION. **** SQL CURSOR DECLARATIONS **** EXEC SQL DECLARE CR1 CURSOR FOR SELECT PROJNO,PROJNAME, DEPTNO,LEADER FROM PROJECT WHERE DEPTNO = :DEPT-NO END-EXEC. PROCEDURE DIVISION. PERFORM SCREEN-RETRIEVE. EXEC SQL OPEN CR1 END-EXEC. When the OPEN statement is encountered, the SELECT in the cursor declaration is executed.

The OPEN cursor statement not only executes the SELECTion of data from the DB2 data base, but it also establishes the initial position of the cursor in the results table. The cursor is placed ready to fetch the first row of data.

Fetch rows of data Once the cursor is open, you can fetch the rows of the results table into program variables for calculations or to be printed. The cursor name is the same as the name of the cursor defined in the DECLARE CURSOR statement.

DATA DIVISION. : : · :. EXEC SQL DECLARE CR1 CURSOR FOR SELECT SALARY FROM EMPLOYEE WHERE DEPTNO = :DEPT-NO END-EXEC. : : · :. PROCEDURE DIVISION. PERFORM SCREEN-RETRIEVE. EXEC SQL OPEN CR1 END-EXEC. EXEC SQL FETCH CR1 INTO :EMP-SAL END-EXEC. IF SQLCODE NOT = 0 PERFORM END-FETCH ELSE PERFORM SUM-AND-FETCH UNTIL SQLCODE NOT = 0. SUM-AND-FETCH. ADD EMP-SAL TO TOTAL-SAL. EXEC SQL FETCH CR1 INTO :EMP-SAL END-EXEC.

Rem: The host variable should have matching data types to the columns returned. If there is more than one column being retrieved, the host variables are listed in the same order as the corresponding fields listed in the cursor declaration. The list of host variables are separated by commas and preceded by colons.

This declaration was used to define a cursor. EXEC SQL DECLARE CR1 CURSOR FOR SELECT EMPNO, LNAME, DEPTNO FROM EMPLOYEE WHERE DEPTNO = :DEPT-NO END-EXEC. Which of the following FETCH statements would be correct to use with this declaration? FETCH CR1 INTO :EM-NO, :DP-NAME, :DP-NO, :DP-MGR FETCH CR1 INTO :DP-NAME, :EM-NO, :DP-NO FETCH CR1 INTO :EM-NO, :EM-NAME, :DP-NO FETCH CR1 INTO :DP-NO, :EM-NO, :EM-NAME

FETCH cursor name INTO host-var1, host-var2... ¦ Positions a cursor on the next row and assigns values of that row to host variables. ¦ Each time the FETCH statement is executed, the position of the cursor is advanced to the next row of the active set and the columns of that row are placed into host variables. ¦ Will be in some kind of loop where each row is dealt with in turn.

Closing the Cursor ¦ Closing the cursor releases the system resources associated with maintaining the active set. ¦ The active set becomes undefined and the rows become unavailable to your application. ¦ If you forget to close the cursor DB2 will do it for you when the job ends, but it is a good idea to close the cursor when you are done with it. ¦ You can close and reopen a cursor as many times as you like. ¦ The syntax is: EXEC SQL CLOSE cursor name END-EXEC.

DATA DIVISION. : : · :. EXEC SQL DECLARE CR1 CURSOR FOR SELECT SALARY FROM EMPLOYEE WHERE DEPTNO = :DEPT-NO END-EXEC. : : · :. PROCEDURE DIVISION. PERFORM SCREEN-RETRIEVE. EXEC SQL OPEN CR1 END-EXEC. EXEC SQL FETCH CR1 INTO :EMP-SAL END-EXEC. IF SQLCODE NOT = 0 PERFORM END-FETCH ELSE PERFORM SUM-AND-FETCH UNTIL SQLCODE NOT = 0. SUM-AND-FETCH. ADD EMP-SAL TO TOTAL-SAL. EXEC SQL EXEC SQL FETCH CR1 CLOSE CR1 INTO :EMP-SAL END-EXEC. END-EXEC

There are several things to watch for when using a cursor: ¦ The number of names in the host list must be equal to the number of columns in the SELECT clause of the DECLARE CURSOR statement. ¦ Check SQLCODE after each FETCH is executed means the active set is empty - 0 means the FETCH executed successfully

Let's review the steps we have learned for using a cursor to select multiple rows: ¦ Declare a cursor defines a cursor using a standard SQL SELECT statement. ¦ Open the cursor opens a cursor so that it can be used to fetch rows from its active set. ¦ Fetch rows of data positions a cursor on the next row of its active set and assigns the values of that row to the host variables. ¦ Stop fetching rows by using the value passed in the SQLCODE to determine when all rows from the active set have been fetched. ¦ Close the cursor to destroy the temporary table created when a cursor was opened.