Database Programming with PL/SQL

Similar documents
Database Programming with PL/SQL

Introduction to Explicit Cursors. Copyright 2008, Oracle. All rights reserved.

Database Programming with PL/SQL

Database Programming with PL/SQL

Database Foundations. 6-4 Data Manipulation Language (DML) Copyright 2015, Oracle and/or its affiliates. All rights reserved.

RETRIEVING DATA USING THE SQL SELECT STATEMENT

Retrieving Data Using the SQL SELECT Statement. Copyright 2004, Oracle. All rights reserved.

Retrieving Data Using the SQL SELECT Statement. Copyright 2009, Oracle. All rights reserved.

Retrieving Data Using the SQL SELECT Statement. Copyright 2004, Oracle. All rights reserved.

Manipulating Data. Copyright 2004, Oracle. All rights reserved.

Database Programming with SQL

Data Manipulation Language

Database Programming with PL/SQL

Database Programming with PL/SQL

Database Programming with SQL

Táblák tartalmának módosítása. Copyright 2004, Oracle. All rights reserved.

Database Programming with PL/SQL

EXISTS NOT EXISTS WITH

Database Programming with SQL

Course Overview. Copyright 2010, Oracle and/or its affiliates. All rights reserved.

Lab Assignment 9 CIS 208A PL/SQL Programming and SQL

Persistent Stored Modules (Stored Procedures) : PSM

Retrieving Data from Multiple Tables

Displaying Data from Multiple Tables. Copyright 2004, Oracle. All rights reserved.

Oracle 1z z0-146 Oracle Database 11g: Advanced PL/SQL. Practice Test. Version QQ:

Database Programming with PL/SQL

DEFAULT Values, MERGE, and Multi-Table Inserts. Copyright 2009, Oracle. All rights reserved.

Alkérdések II. Copyright 2004, Oracle. All rights reserved.

Creating Other Schema Objects

IT Certification Exams Provider! Weofferfreeupdateserviceforoneyear! h ps://

Introduction to Oracle9i: SQL

Database Foundations. 6-9 Joining Tables Using JOIN. Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Creating Other Schema Objects. Copyright 2004, Oracle. All rights reserved.

Actual4Test. Actual4test - actual test exam dumps-pass for IT exams

The following practical s have to be performed and journal should contain the output with aim as per syllabus. This is just for reference

Sisteme Informatice şi Standarde Deschise (SISD) Curs 7 Standarde pentru programarea bazelor de date (1)

Limit Rows Selected. Copyright 2008, Oracle. All rights reserved.

HP NonStop Structured Query Language (SQL)

Oracle Database 11g: SQL Fundamentals I

Oracle MOOC: SQL Fundamentals

Assignment Grading Rubric

Oracle Internal & Oracle Academy

Introduction to Computer Science and Business

Oracle PLSQL. Course Summary. Duration. Objectives

Restricting and Sorting Data. Copyright 2004, Oracle. All rights reserved.

Oracle Database 11g: PL/SQL Fundamentals

KATHERYNE MERIVETH AMARO MEDRANO. this Student Guide

Advance Database Systems. Joining Concepts in Advanced SQL Lecture# 4

Database Programming with PL/SQL

Three types of sub queries are supported in SQL are Scalar, Row and Table sub queries.

Sample Test #1 Questions

1Z0-144 Q&As Oracle Database 11g: Program with PL/ SQL

MIS 409 PL/SQL test (May 5, 2016) 1) Create a package called Company_package - This package consists of two procedures and two functions.

REF Cursors. Oracle University and Egabi Solutions use only

Using the Set Operators. Copyright 2006, Oracle. All rights reserved.

Database Programming with PL/SQL

SELF TEST. List the Capabilities of SQL SELECT Statements

Lecture 08. Spring 2018 Borough of Manhattan Community College

Question Bank PL/SQL Fundamentals-I

Contents I Introduction 1 Introduction to PL/SQL iii

SYSTEM CODE COURSE NAME DESCRIPTION SEM

GIFT Department of Computing Science. CS-217/224: Database Systems. Lab-5 Manual. Displaying Data from Multiple Tables - SQL Joins

Misc. Triggers Views Roles Sequences - Synonyms. Eng. Mohammed Alokshiya. Islamic University of Gaza. Faculty of Engineering

Using the Set Operators. Copyright 2004, Oracle. All rights reserved.

Intermediate SQL: Aggregated Data, Joins and Set Operators

Database Programming with SQL

DATA CONSTRAINT. Prepared By: Dr. Vipul Vekariya

Lesson B Objectives IF/THEN. Chapter 4B: More Advanced PL/SQL Programming

Introduction to Relational Database Concepts. Copyright 2011, Oracle. All rights reserved.

Bsc (Hons) Software Engineering. Examinations for / Semester 1. Resit Examinations for BSE/15A/FT & BSE/16A/FT

Oracle Database 10g: PL/SQL Fundamentals. Oracle Internal & Oracle Academy Use Only. Volume I Student Guide. D17112GC30 Edition 3.0 April 2009 D59413

Updating Column Values and Deleting Rows. Copyright 2008, Oracle. All rights reserved.

Question No : 1 Which statement is true about triggers on data definition language (DDL) statements?

A subquery is a nested query inserted inside a large query Generally occurs with select, from, where Also known as inner query or inner select,

5. Single-row function

Using DbVisualizer Variables

Restricting and Sorting Data. Copyright 2004, Oracle. All rights reserved.

Oracle Class VI. Exception Block Cursors For Loops

Database Programming - Section 11. Instructor Guide

Oracle PL/SQL Best Practices Part 1. John Mullins

Oracle Database 11g: SQL and PL/SQL Fundamentals

KORA. RDBMS Concepts II

Database Programming with PL/SQL

Graphical Joins in More Detail

2 PL/SQL - fundamentals Variables and Constants Operators SQL in PL/SQL Control structures... 7

COMP 430 Intro. to Database Systems. Encapsulating SQL code

Oracle Database 11g: Develop PL/SQL Program Units

Foreword Preface Db2 Family And Db2 For Z/Os Environment Product Overview DB2 and the On-Demand Business DB2 Universal Database DB2 Middleware and

The Tie That Binds: An Introduction to ADF Bindings

Database Foundations. 6-1 Introduction to Oracle Application Express. Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Database Programming with PL/SQL

Oracle Database: SQL and PL/SQL Fundamentals NEW

Oracle Database: SQL and PL/SQL Fundamentals Ed 2

Database Programming with PL/SQL

Oracle 1Z Oracle Database 11g : SQL Fundamentals I. Download Full Version :

WHAT IS SQL. Database query language, which can also: Define structure of data Modify data Specify security constraints

BraindumpsVCE. Best vce braindumps-exam vce pdf free download

HR Database. Sample Output from TechWriter 2007 for Databases

Self-Demo Guide. Oracle ilearning and HTML DB

INDEX. 1 Introduction. 2 Types of Cursors 2.1 Explicit cursor 2.2 Attributes 2.3 Implicit cursor 2.4 Attributes. 3 Parameterized cursor

Transcription:

Database Programming with PL/SQL 5-6

Objectives This lesson covers the following objectives: Explain the need for using multiple cursors to produce multilevel reports Create PL/SQL code to declare and manipulate multiple cursors within nested loops Create PL/SQL code to declare and manipulate multiple cursors using parameters 3

Purpose In real-life programs you often need to declare and use two or more cursors in the same PL/SQL block. Often these cursors are related to each other by parameters. One common example is the need for multi-level reports in which each level of the report uses rows from a different cursor. This lesson does not introduce new concepts or syntax. It shows more powerful uses for the concepts and syntax that you already know. 4

A Sample Problem Statement You need to produce a report that lists each department as a sub-heading, immediately followed by a listing of the employees in that department, followed by the next department, and so on. You need two cursors, one for each of the two tables. The cursor based on EMPLOYEES is opened several times, once for each department. 5

Problem Solution: Step 1 Declare two cursors, one for each table, plus associated record structures. Why is cursor cur_emp declared with a parameter? DECLARE CURSOR cur_dept IS SELECT department_id, department_name FROM departments ORDER BY department_name; CURSOR cur_emp (p_deptid NUMBER) IS SELECT first_name, last_name FROM employees WHERE department_id = p_deptid ORDER BY last_name; v_deptrec cur_dept%rowtype; v_emprec cur_emp%rowtype; 6

Problem Solution: Step 2 Open the cur_dept cursor and fetch and display the DEPARTMENTS rows in the usual way. DECLARE CURSOR cur_dept IS...; CURSOR cur_emp (p_deptid NUMBER) IS...; v_deptrec cur_dept%rowtype; v_emprec cur_emp%rowtype; BEGIN OPEN cur_dept; LOOP FETCH cur_dept INTO v_deptrec; EXIT WHEN cur_dept%notfound; DBMS_OUTPUT.PUT_LINE(v_deptrec.department_name); CLOSE cur_dept; END; 7

Problem Solution: Step 3 After each DEPARTMENTS row has been fetched and displayed, you need to fetch and display the EMPLOYEES in that department. To do this, you open the EMPLOYEES cursor, fetch and display its rows in a nested loop, and close the cursor. Then, you do the same for the next DEPARTMENTS row. And so on. The next slide shows the code for this. 8

Problem Solution If the cursor is based on a join of two tables, we may want to lock the rows of one table but not the other. To do this, we specify any column of the table we want to lock. DECLARE CURSOR cur_dept IS...; CURSOR cur_emp (p_deptid NUMBER) IS...; v_deptrec cur_dept%rowtype; v_emprec cur_emp%rowtype; BEGIN OPEN cur_dept; LOOP FETCH cur_dept INTO v_deptrec; EXIT WHEN cur_dept%notfound; DBMS_OUTPUT.PUT_LINE(v_deptrec.department_name); OPEN cur_emp (v_deptrec.department_id); LOOP FETCH cur_emp INTO v_emprec; EXIT WHEN cur_emp%notfound; DBMS_OUTPUT.PUT_LINE(v_emprec.last_name ' ' v_emprec.first_name); CLOSE cur_emp; CLOSE cur_dept; END; 9

A Second Example You need to produce a report that lists each location in which your departments are situated, followed by the departments in that location. Again, you need two cursors, one for each of the two tables. The cursor based on DEPARTMENTS will be opened several times, once for each location. The next slide shows the code needed to produce this report. 10

A Second Example DECLARE CURSOR cur_loc IS SELECT * FROM locations; CURSOR cur_dept (p_locid NUMBER) IS SELECT * FROM departments WHERE location_id = p_locid; v_locrec cur_loc%rowtype; v_deptrec cur_dept%rowtype; BEGIN OPEN cur_loc; LOOP FETCH cur_loc INTO v_locrec; EXIT WHEN cur_loc%notfound; DBMS_OUTPUT.PUT_LINE(v_locrec.city); OPEN cur_dept (v_locrec.location_id); LOOP FETCH cur_dept INTO v_deptrec; EXIT WHEN cur_dept%notfound; DBMS_OUTPUT.PUT_LINE(v_deptrec.department_name); CLOSE cur_dept; CLOSE cur_loc; END; 11

Using FOR Loops with Multiple Cursors You can use FOR loops (and other cursor techniques, such as FOR UPDATE) with multiple cursors, just as you can with single cursors. DECLARE CURSOR cur_loc IS SELECT * FROM locations; CURSOR cur_dept (p_locid NUMBER) IS SELECT * FROM departments WHERE location_id = p_locid; BEGIN FOR v_locrec IN cur_loc LOOP DBMS_OUTPUT.PUT_LINE(v_locrec.city); FOR v_deptrec IN cur_dept (v_locrec.location_id) LOOP DBMS_OUTPUT.PUT_LINE(v_deptrec.department_name); END; 12

A Final Example Which employees will receive a salary increase by running the code below? DECLARE CURSOR cur_dept IS SELECT * FROM my_departments; CURSOR cur_emp (p_dept_id NUMBER) IS SELECT * FROM my_employees WHERE department_id = p_dept_id FOR UPDATE NOWAIT; BEGIN FOR v_deptrec IN cur_dept LOOP DBMS_OUTPUT.PUT_LINE(v_deptrec.department_name); FOR v_emprec IN cur_emp (v_deptrec.department_id) LOOP DBMS_OUTPUT.PUT_LINE(v_emprec.last_name); IF v_deptrec.location_id = 1700 AND v_emprec.salary < 10000 THEN UPDATE my_employees SET salary = salary * 1.1 WHERE CURRENT OF cur_emp; END IF; END; 13

Summary In this lesson, you should have learned how to: Explain the need for using multiple cursors to produce multilevel reports Create PL/SQL code to declare and manipulate multiple cursors within nested loops Create PL/SQL code to declare and manipulate multiple cursors using parameters 14