Database Programming Using Oracle 11g

Slides:



Advertisements
Similar presentations
AN INTRODUCTION TO PL/SQL Mehdi Azarmi 1. Introduction PL/SQL is Oracle's procedural language extension to SQL, the non-procedural relational database.
Advertisements

CS0004: Introduction to Programming Repetition – Do Loops.
Loops (Part 1) Computer Science Erwin High School Fall 2014.
Basic Control Structures Control order of execution of statements sequential selection iteration - Repeat some action while a certain condition is true.
Computer programming Lecture 3. Lecture 3: Outline Program Looping [Kochan – chap.5] –The for Statement –Relational Operators –Nested for Loops –Increment.
PL/SQL (Procedural Language extensions to SQL) Prepared by: Manoj Kathpalia Edited by: M V Ramakrishna.
Using Oracle PL/SQL PL/SQL stands for Procedural Language/SQL. PL/SQL extends SQL by adding constructs found in procedural languages, resulting in a structural.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
Introduction to Computer Programming in c
Introduction to PL/SQL. Procedural Language extension for SQL Oracle Proprietary 3GL Capabilities Integration of SQL Portable within Oracle data bases.
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
Chapter 5: Control Structures II (Repetition)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
Lecture 4 PL/SQL language. PL/SQL – procedural SQL Allows combining procedural and SQL code PL/SQL code is compiled, including SQL commands PL/SQL code.
08/10/ Iteration Loops For … To … Next. 208/10/2015 Learning Objectives Define a program loop. State when a loop will end. State when the For.
PL/SQL Loops. Building Logical Conditions All logical conditions must yield a boolean condition. You can build a simple Boolean condition by combining.
Program with PL/SQL. Interacting with the Oracle Server.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Program with PL/SQL Lesson 4. Writing Control Structure.
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
Chapter 8 Iteration Dept of Computer Engineering Khon Kaen University.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
PL/SQL. Introduction to PL/SQL block Declare declarations Begin executable statement Exception exception handlers End;
Bordoloi and Bock Control Structures: Iterative Control.
1 Chapter 9. To familiarize you with  Simple PERFORM  How PERFORM statements are used for iteration  Options available with PERFORM 2.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 10 - JavaScript/JScript: Control Structures II Outline 10.1Introduction 10.2Essentials of.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
1 Writing Control Structures Part D. 2 PL/SQL Decision Control Structures Sequential processing Sequential processing Processes statements one after another.
1 do-while Statement 2 Do-While Statement Is a looping control structure in which the loop condition is tested after each iteration of the loop. SYNTAX.
Repetition Control Structure. Introduction Many applications require certain operations to be carried out more than once. Such situations require repetition.
1 Looping Dale/Weems/Headington. 2 KA/JS/P Warning l Save your work often! l In the Khan Academy, JavaScript environment, infinite loops will lock up.
Topic: Control Statements. Recap of Sequence Control Structure Write a program that accepts the basic salary and allowance amount for an employee and.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
REPETITION MTS3033 OBJECT ORIENTED PROGRAMMING 1.
COMP Loop Statements Yi Hong May 21, 2015.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Catie Welsh February 9,  Friday - No Lab! ◦ Bring questions on Project 2  Lab 3 due on Friday 2.
Computer C programming Chapter 3. CHAPTER 3 Program Looping –The for Statement –Nested for Loops –for Loop Variants –The while Statement –The do Statement.
Lecture 4b Repeating With Loops
Chapter 6: Loops.
Program with PL/SQL Lesson 4.
REPETITION CONTROL STRUCTURE
Writing Control Structures
Oracle11g: PL/SQL Programming Chapter 2 Basic PL/SQL Block Structures.
SQL PL/SQL Presented by: Dr. Samir Tartir
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Control Structure Senior Lecturer
Arrays, For loop While loop Do while loop
Outline Altering flow of control Boolean expressions
Writing Control Structures
Algorithm Discovery and Design
Iteration: Beyond the Basic PERFORM
Iteration: Beyond the Basic PERFORM
3 Control Statements:.
LOOPS BY: LAUREN & ROMEO.
Repetition Control Structure
Chapter 6: Repetition Statements
Computer Science Core Concepts
Computer programming Lecture 3.
Chapter 5: Control Structures II (Repetition)
Loops.
PROGRAM FLOWCHART Iteration Statements.
Chap 7. Advanced Control Statements in Java
Database Programming Using Oracle 11g
Database Programming Using Oracle 11g
Database Programming Using Oracle 11g
Database Programming Using Oracle 11g
Looping and Repetition
Presentation transcript:

Database Programming Using Oracle 11g Iterative Control-I

Iterative Control-I Iterative Control-I Simple Loop

Iterative Control-I Simple Loop Structure to repeat statements Sequence are statements are executed in each iteration. Need exit statement to terminate the loop

Iterative Control-I Simple Loop Syntax Loop without exit condition is an infinite loop. Loop exit if exit condition evaluate to true. Different from other loops

Iterative Control-I Simple Loop Syntax

Iterative Control-I Loop Statement -1; Statement -2; Exit | Exit When condition is true; End loop; End;

Iterative Control-I Implementing Simple Loop -1

Infinite Loop but syntactically correct, no exit condition Iterative Control-I declare rep number(10):=0; begin loop dbms_output.put_line (rep); end loop; end; Infinite Loop but syntactically correct, no exit condition

Iterative Control-I Implementing Simple Loop -II

Iterative Control-I declare rep number(10):=0; begin loop if rep >=5 then dbms_output.put_line ('Value of Rep: ' || rep); exit; end if;

Iterative Control-I rep:=rep+1; end loop; end;

Iterative Control-I Implementing Simple Loop with SQL -I

Iterative Control-I Task to do: Write a PL/SQL block to insert 10 row in emp table , add data in empno column only with starting value of 0 and ending value of 10.

Iterative Control-I declare rep number(10):=0; begin loop insert into emp (empno) values (rep); rep:=rep+1; exit when rep > 10 ; end loop; end;

Iterative Control-I Implementing Simple Loop with SQL -II

Iterative Control-I Task to do: Write a PL/SQL block to display the reverse of maximum salary from emp table.

Iterative Control-I DECLARE N NUMBER(5):=0; REV NUMBER(5):=0; R NUMBER(5):=0; total_emp number(10):=0; BEGIN select max(sal) into n from emp; Dbms_output.put_line('maximum Salary is : ' || n);

Iterative Control-I LOOP R:=MOD(N,10); --return reminder REV:=REV*10+R; N:=TRUNC(N/10); exit when n = 0; END LOOP; Dbms_output.put_line(‘Reverse Value : ‘ || rev); END;

Iterative Control-I While Loop

Iterative Control-I While Loop Structure to repeat statements Sequence are statements are executed in each iteration. Exit when condition is evaluated to false.

Iterative Control-I While Loop Syntax

Iterative Control-I WHILE condition LOOP sequence_of_statements END LOOP;

Iterative Control-I Implementing While Loop - I

Iterative Control-I declare done BOOLEAN := TRUE; BEGIN WHILE done LOOP DBMS_OUTPUT.PUT_LINE ('This line got printed once only'); done := FALSE; END LOOP; end;

Iterative Control-I Implementing While Loop - II

Iterative Control-I declare counter number(4) := 1; begin while counter <= 8 loop dbms_output.put_line(counter); If counter=4 then Exit; -- Loop will be terminated End if;

Iterative Control-I counter := counter +1; End loop; end; Output: 1,2,3,4 Problem: Loop will be terminated without reaching false condition

Iterative Control-I declare counter number(4) := 1; begin while counter <= 4 loop dbms_output.put_line(counter); If counter=5 then Exit; End if;

Iterative Control-I counter := counter +1; End loop; end; Output: 1,2,3,4

Iterative Control-I Implementing While Loop - III

Iterative Control-I Write a PL/SQL block to insert 10 row in emp table using while loop, populate only empno column only with starting value of 0 and ending value of 10.

Iterative Control-I declare rep number(10):=0; begin while ( rep < 10 ) loop insert into emp (empno) values (rep); rep:=rep+1; dbms_output.put_line (' Rows are inserted in the loop with row no ' || rep);

Iterative Control-I end loop; dbms_output.put_line (' Transaction Completed '); end;

Iterative Control-I Implementing While Loop - IV

Iterative Control-I Task to do: Write a PL/SQL block to calculate the sum of first 100 even integers starting from 1

Iterative Control-I declare counter number(4):=2; total number(5):=0; begin while counter < 100 loop total:=total+counter; dbms_output.put_line (' Current Sum is : ' || total ); counter:=counter+2;

Iterative Control-I end loop; dbms_output.put_line (' Sum of even integers between 1 and 100 is ' || total ); end;

Iterative Control-I Implementing While Loop using SQL - I

Iterative Control-I Task to do: Write a PL/SQL block to calculate the total of first 100 even integers starting from 1, at any point in time if total exceeds 400 and remain less than 800, add a new row in temp table with columns of sum, currentdate and status.

Iterative Control-I declare counter number(4):=2; total number(5):=0; status varchar2(80):=''; begin while counter < 100 loop total:=total+counter; dbms_output.put_line (' Current Sum is : ' || total );

Iterative Control-I dbms_output.put_line (' Current Sum is : ' || total ); counter:=counter+2; if (total > 400 and total < 800) then status:='Total is greater than 400 and less than 800'; insert into temp values (total, sysdate,status); dbms_output.put_line ('Row Inserted'); end if;

Iterative Control-I end loop; dbms_output.put_line ('Total of even integers between 1 and 100 is ' || total ); end;

Iterative Control-I Do-While Loop

Iterative Control-I Do - While Loop Loop should be executed at least once. No direct implementation is provided in PL/SQL Can be implemented using simple loop

Iterative Control-I Implementing Do While Loop

Iterative Control-I declare current_val number(10):=0; begin loop dbms_output.put_line ('Value of count is : ' || current_val); current_val:=current_val+1; exit when current_val> 10 ; end loop; end;

Iterative Control-I Numeric For Loop Use to repeat the set of operations. Numeric because it require an integer as its terminating Control variable is initialized with starting value

Iterative Control-I Numeric For Loop Interation count is by default one Iteration count can’t be changed in the body of the For Loop Starting value in the loop should be less then end value for loop to continue

Iterative Control-I Numeric For Loop Syntax

Iterative Control-I FOR counter IN [ Reverse ] lower_limit..upper_limit LOOP --Value of counter can’t be changed in the body -- increment of one in counter is fixed statements; END LOOP;

Iterative Control-I Implementing Numeric For Loop - I

Iterative Control-I DECLARE i NUMBER := 100; BEGIN FOR i IN 1..10 LOOP dbms_output.put_line ( ‘ Value of I : ‘ ||i); i:=i+1; -- Error end loop; End;

Iterative Control-I Implementing Numeric For Loop - II

Iterative Control-I DECLARE start_val NUMBER := 10; end_val NUMBER := 0; i number:=0; BEGIN FOR i IN start_val..end_val LOOP dbms_output.put_line('Value of i : '|| i); END LOOP; END;

Iterative Control-I Implementing Numeric For Loop - III

Iterative Control-I To do task: Write a PL/SQL block to insert rows in temp table, if the row inserted is odd then store message ‘Value is odd’ and if the row added is even then display message ‘ Value is even’

Iterative Control-I DECLARE x NUMBER := 100; i number:=0; BEGIN FOR i IN 1..10 LOOP IF MOD(i,2) = 0 THEN -- i is even dbms_output.put_line('Value of i is even'); INSERT INTO temp VALUES (i, x, 'i is even');

Iterative Control-I ELSE INSERT INTO temp VALUES (i, x, 'i is odd'); dbms_output.put_line('Value of i is odd'); END IF; x := x + 100; END LOOP; COMMIT; END;

Iterative Control-I For Loop with Reverse Option Use to repeat the set of operations. Control variable in initialized with end value Value is decrease everytime by one

Iterative Control-I For Loop with Reverse Option Value of upper limit should be greater than lower limit to continue Iteration count can’t be changed in the body of the For Loop

Iterative Control-I Implementing Numeric Reverse For Loop - I

Iterative Control-I DECLARE loop_start Integer := 1; BEGIN FOR i IN REVERSE loop_start..5 LOOP DBMS_OUTPUT.PUT_LINE('Loop counter is ' || i); END LOOP; END;

Iterative Control-I Implementing Numeric Reverse For Loop - II

Iterative Control-I DECLARE 2 loop_start Integer := 5; BEGIN FOR i IN REVERSE loop_start..1 LOOP DBMS_OUTPUT.PUT_LINE('Loop counter is ' || i); END LOOP; END;

Iterative Control-I Implementing Reverse For Loop with SQL-I

Iterative Control-I To do task: Write a PL/SQL block to insert rows in temp table, if the row inserted is odd then store message ‘Value is odd’ and if the row added is even then display message ‘ Value is even’

Iterative Control-I DECLARE x NUMBER := 100; i number:=0; BEGIN FOR i IN REVERSE 1..10 LOOP IF MOD(i,2) = 0 THEN -- i is even dbms_output.put_line('Value of i is even‘ || i); INSERT INTO temp VALUES (i, x, 'i is even');

Iterative Control-I ELSE INSERT INTO temp VALUES (i, x, 'i is odd‘ || i); dbms_output.put_line('Value of i is odd'); END IF; x := x + 100; END LOOP; COMMIT; END;