Oracle PL/SQL Loops Please use speaker notes for additional information!

Slides:



Advertisements
Similar presentations
Programming with App Inventor Computing Institute for K-12 Teachers Summer 2012 Workshop.
Advertisements

CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
Playing computer with logic problems Please use speaker notes for additional information!
AN INTRODUCTION TO PL/SQL Mehdi Azarmi 1. Introduction PL/SQL is Oracle's procedural language extension to SQL, the non-procedural relational database.
PL/SQL. Introduction to PL/SQL PL/SQL is the procedure extension to Oracle SQL. It is used to access an Oracle database from various environments (e.g.
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.
Library example CREATE TABLE books ( isbn VARCHAR2(13) PRIMARY KEY, title VARCHAR2(200), summary VARCHAR2(2000), author VARCHAR2(200), date_published DATE,
PL/SQL (Procedural Language extensions to SQL) Prepared by: Manoj Kathpalia Edited by: M V Ramakrishna.
Lecture-5 Though SQL is the natural language of the DBA, it suffers from various inherent disadvantages, when used as a conventional programming language.
Chapter 4B: More Advanced PL/SQL Programming
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.
CSE 113 Week 5 February , Announcements  Module 2 due 2/15  Exam 3 is on 2/15  Module 3 due 2/22  Exam 4 is on 2/25  Module 4 due 2/29.
Introduction To Form Builder
Introduction to PL/SQL Chapter 9. Objectives Explain the need for PL/SQL Explain the benefits of PL/SQL Identify the different types of PL/SQL blocks.
PL/SQL Bulk Collections in Oracle 9i and 10g Kent Crotty Burleson Consulting October 13, 2006.
Simple Python Loops Sec 9-7 Web Design.
Introduction to PL/SQL. Procedural Language extension for SQL Oracle Proprietary 3GL Capabilities Integration of SQL Portable within Oracle data bases.
Session Title: Using SQL and PL/SQL for Queries and Reporting Presented By: Stephen Frederic Institution: IHL September 16, 2013.
PL / SQL P rocedural L anguage / S tructured Q uery L anguage Chapter 7 in Lab Reference.
Stored Procedures, Triggers, Program Access Dr Lisa Ball 2008.
While Loops Indefinite Iteration. Last lesson we looked at definite loops using the ‘For’ statement. The while loop keeps going while some condition is.
For Loops (ProjFor1, ProjFor2, ProjFor3, ProjFor4, textbox, textbox1) Please use speaker notes for additional information!
In the next step you will enter some data records into the table. This can be done easily using the ‘Data Browser’. The data browser can be accessed via.
PL/SQL Loops. Building Logical Conditions All logical conditions must yield a boolean condition. You can build a simple Boolean condition by combining.
Logic Structure - focus on looping Please use speaker notes for additional information!
1 Copyright © 2004, Oracle. All rights reserved. Introduction to PL/SQL.
Stored procedures1 Stored procedures and functions Procedures and functions stored in the database.
University of Sunderland COM 220 Lecture Six Slide 1 Building Interactive Forms Applications using Oracle.
Session Title: Using SQL and PL/SQL for Queries and Reporting Presented By: Stephen Frederic Institution: IHL September 16, 2014.
1 Copyright © 2004, Oracle. All rights reserved. Introduction to PL/SQL.
Chapter 15 Introduction to PL/SQL. Chapter Objectives  Explain the benefits of using PL/SQL blocks versus several SQL statements  Identify the sections.
PL/SQL. Introduction to PL/SQL block Declare declarations Begin executable statement Exception exception handlers End;
Guide to Oracle 10g ITBIS373 Database Development Lecture 4a - Chapter 4: Using SQL Queries to Insert, Update, Delete, and View Data.
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.
Oracle 8i Exception Handling. General Syntax DECLARE --- BEGIN --- EXCEPTION WHEN exception_name1 THEN -Error handling statements WHEN exception_name2.
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.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Handling Exceptions. Objectives What is exception Types of exceptions How to handle exceptions Trapping pre defined oracle errors.
PL/SQL programming Procedures and Cursors Lecture 1 [Part 2]
Advanced Databases More Advanced PL/SQL Programing 1.
Oracle10g Developer: PL/SQL Programming1 Objectives Named program units How to identify parameters The CREATE PROCEDURE statement Creating a procedure.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS 1 Copyright: 2015 Illinois Institute of Technology_ George Koutsogiannakis.
CS422 Principles of Database Systems Oracle PL/SQL Chengyu Sun California State University, Los Angeles.
CS422 Principles of Database Systems Stored Procedures and Triggers Chengyu Sun California State University, Los Angeles.
This is a while loop. The code is done while the condition is true. The code that is done is enclosed in { }. Note that this program has three parts: Housekeeping.
PL/pgSQL
Data Virtualization Tutorial: Introduction to SQL Script
Difference between Oracle PL/SQL and MySQL
Introduction to Triggers
More on Procedures (Internal/Local procedures)
Handling Exceptions.
SQL PL/SQL Presented by: Dr. Samir Tartir
Error Handling Summary of the next few pages: Error Handling Cursors.
PL/SQL Scripting in Oracle:
Advanced PL/SQL Programing
Please use speaker notes for additional information!
PL/SQL Programing : Triggers
Procedures.
Please use speaker notes for additional information!
Handling Exceptions.
Excel 2010 Functions A function is a predefined formula that performs a calculation using specific values in a particular order. Functions save you time.
More and Still More on Procedures and Functions
4.3 Adding Modules to a Page
Handling Data in PL/SQL Blocks
Database Programming Using Oracle 11g
© 2014, Mike Murach & Associates, Inc.
Database Programming Using Oracle 11g
Presentation transcript:

Oracle PL/SQL Loops Please use speaker notes for additional information!

DECLARE v_recno testloop.rec_no%TYPE :=1; v_ctr testloop.ctr%TYPE :=100; BEGIN LOOP INSERT INTO testloop(rec_no, ctr) VALUES(v_recno, v_ctr); v_recno :=v_recno + 1; v_ctr := v_ctr + 10; IF v_recno > 5 THEN EXIT; END IF; END LOOP; END; / SQL> edit basicloop1 Basic loop SYNTAX: LOOP Processing code within the loop EXIT (which can include a WHEN condition) END LOOP; SQL> SELECT * FROM testloop; REC_NO CTR DATA_IN basicloop1 PL/SQL procedure successfully completed. PL/SQL code for the basicloop1 block (as seen in the editor).

DECLARE v_recno testloop.rec_no%TYPE :=1; v_ctr testloop.ctr%TYPE :=100; BEGIN LOOP INSERT INTO testloop(rec_no, ctr) VALUES(v_recno, v_ctr); v_recno :=v_recno + 1; v_ctr := v_ctr + 10; IF v_recno > 5 THEN EXIT; END IF; END LOOP; END; / Pass Rows in testloop v_recno v_ctr test v_recno > 5 Initialize Pass > 5 = NO - LOOP Pass > 5 = NO - LOOP Pass > 5 = NO - LOOP Pass > 5 = NO - LOOP Pass > 5 = YES - EXIT Basic loop

DECLARE v_recno testloop.rec_no%TYPE :=1; v_ctr testloop.ctr%TYPE :=100; BEGIN LOOP INSERT INTO testloop(rec_no, ctr) VALUES(v_recno, v_ctr); v_recno :=v_recno + 1; v_ctr := v_ctr + 10; EXIT WHEN v_recno > 5 END LOOP; END; / Pass Rows in testloop v_recno v_ctr test v_recno > 5 Initialize Pass > 5 = NO - LOOP Pass > 5 = NO - LOOP Pass > 5 = NO - LOOP Pass > 5 = NO - LOOP Pass > 5 = YES - EXIT Basic loop

Basicloop1 v_recno = 1 v_ctr = 100 Loop Insert v_recno, v_ctr into testloop Add 1 to v_recno Add 1 to v_ctr v_recno NOT > 5 END Basicloop1 Loop Y N Pass Rows in testloop v_recno v_ctr test v_recno NOT > 5 Initialize Pass NOT> 5 = NO - LOOP Pass NOT > 5 = NO - LOOP Pass NOT > 5 = NO - LOOP Pass NOT > 5 = NO - LOOP Pass > 5 = YES - EXIT Exit drops to END processing complete END has been reached - processing complete END Directions: Click to initialize and then click to see each of the passes. The last message will tell you processing is complete.

While loops DECLARE v_recno testloop.rec_no%TYPE :=1; v_ctr testloop.ctr%TYPE :=100; BEGIN WHILE v_recno < 6 LOOP INSERT INTO testloop(rec_no, ctr) VALUES(v_recno, v_ctr); v_recno :=v_recno + 1; v_ctr := v_ctr + 10; END LOOP; END; / SQL> edit whileloop1 whileloop1 PL/SQL procedure successfully completed. SQL> SELECT * FROM testloop; REC_NO CTR DATA_IN SYNTAX: WHILE condition LOOP processing code END LOOP;

While loops DECLARE v_recno testloop.rec_no%TYPE :=1; v_ctr testloop.ctr%TYPE :=100; BEGIN WHILE v_recno < 6 LOOP INSERT INTO testloop(rec_no, ctr) VALUES(v_recno, v_ctr); v_recno :=v_recno + 1; v_ctr := v_ctr + 10; END LOOP; END; / Pass test v>recno recno < 6 rows in testloop v_recno v_ctr Initialize Pass 1 1 < 6 is YES - LOOP Pass 2 2 < 6 is YES - LOOP Pass 3 3 < 6 is YES - LOOP Pass 4 4 < 6 is YES - LOOP Pass 5 5 < 6 is YES - LOOP Pass 6 6 < 6 is NO - END LOOP (processing is not executed)

v_recno < 6 Y N Whileloop1 v_recno = 1 v_ctr = 100 LOOP END Whileloop1 END Insert v_recno, v_ctr into testloop Add 1 to v_recno Add 1 to v_ctr LOOP END LOOP While loop This flowchart shows the loop code in a separate module. If v_recno <6 the loop is executed. Then the control returns to ask the question again. If the answer to v_recno <6 is No, then the loop is not performed and the END is executed since there is no code except END after the loop. The dotted lines would not be included in the flowchart - I put them in to show the flow.

v_recno < 6 Y N Whileloop1 v_recno = 1 v_ctr = 100 LOOP END Whileloop1 END Insert v_recno, v_ctr into testloop Add 1 to v_recno Add 1 to v_ctr LOOP END LOOP While loop Pass test v>recno recno < 6 rows in testloop v_recno v_ctr Initialize Pass 1 1 < 6 is YES - LOOP Pass 2 2 < 6 is YES - LOOP Pass 3 3 < 6 is YES - LOOP Pass 4 4 < 6 is YES - LOOP Pass 5 5 < 6 is YES - LOOP Pass 6 6 < 6 is NO - END LOOP (processing is not executed) Directions: Click to initialize and then click to see each of the passes. The last message will tell you processing is complete. processing complete END has been reached - processing complete

While loop Whileloop1 v_recno = 1 v_ctr = 100 Loop Insert v_recno, v_ctr into testloop Add 1 to v_recno Add 1 to v_ctr END Whileloop1 END End Loop v_recno NOT < 6 Y N End Loop Pass test v>recno recno < 6 rows in testloop v_recno v_ctr Initialize Pass 1 1 < 6 is YES - LOOP Pass 2 2 < 6 is YES - LOOP Pass 3 3 < 6 is YES - LOOP Pass 4 4 < 6 is YES - LOOP Pass 5 5 < 6 is YES - LOOP Pass 6 6 < 6 is NO - END LOOP (processing is not done)

For loop SYNTAX: FOR index in [REVERSE] lower_bound..upper_bound LOOP processing code END LOOP; DECLARE v_recno testloop.rec_no%TYPE :=1; v_ctr testloop.ctr%TYPE :=100; BEGIN FOR i IN 1..5 LOOP INSERT INTO testloop(rec_no, ctr) VALUES(v_recno, v_ctr); v_recno :=v_recno + 1; v_ctr := v_ctr + 10; END LOOP; END; / SQL> edit forloop1 SQL> SELECT * FROM testloop; REC_NO CTR DATA_IN PL/SQL procedure successfully completed.

For loop DECLARE v_recno testloop.rec_no%TYPE :=1; v_ctr testloop.ctr%TYPE :=100; BEGIN FOR i IN 1..5 LOOP INSERT INTO testloop(rec_no, ctr) VALUES(v_recno, v_ctr); v_recno :=v_recno + 1; v_ctr := v_ctr + 10; END LOOP; END; / Pass i range 1 to 5 rows in testloop v_recno v_ctr Initialize Pass 1 i = 1 - LOOP Pass 2 i = 2 - LOOP Pass 3 i = 3 - LOOP Pass 4 i = 4 - LOOP Pass 5 i = 5 - LOOP Maximum value i has been reached - END LOOP (processing is not done)