SlideShare a Scribd company logo
Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
19
Writing Control Structures
19-2 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Objectives
After completing this lesson, you should
be able to do the following:
• Identify the uses and types of control
structures
• Construct an IF statement
• Construct and identify different loop
statements
• Use logic tables
• Control block flow using nested loops
and labels
19-3 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Controlling PL/SQL Flow
of Execution
You can change the logical flow of
statements using conditional IF
statements and loop control structures.
Conditional IF statements:
• IF-THEN-END IF
• IF-THEN-ELSE-END IF
• IF-THEN-ELSIF-END IF
19-4 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
IF Statements
IF condition THEN
statements;
[ELSIF condition THEN
statements;]
[ELSE
statements;]
END IF;
Syntax
Simple IF statement:
Set the manager ID to 22 if the employee
name is Osborne.
IF v_ename = 'OSBORNE' THEN
v_mgr := 22;
END IF;
19-5 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Simple IF Statements
Set the job title to Salesman, the department
number to 35, and the commission to 20%
of the current salary if the last name is
Miller.
Example
. . .
IF v_ename = 'MILLER' THEN
v_job := 'SALESMAN';
v_deptno := 35;
v_new_comm := sal * 0.20;
END IF;
. . .
19-6 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
IF-THEN-ELSE Statement
Execution Flow
IF condition
TRUE
THEN actions
(including further IFs)
FALSE
ELSE actions
(including further IFs)
19-7 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
IF-THEN-ELSE Statements
Set a flag for orders where there are fewer
than five days between order date and
ship date.
Example
...
IF v_shipdate - v_orderdate < 5 THEN
v_ship_flag := 'Acceptable';
ELSE
v_ship_flag := 'Unacceptable';
END IF;
...
19-8 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
IF-THEN-ELSIF
Statement Execution Flow
IF condition
TRUE
THEN actions
FALSE
ELSIF
condition
TRUE
THEN actions
FALSE
ELSE
actions
19-9 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
IF-THEN-ELSIF Statements
For a given value, calculate a percentage
of that value based on a condition.
Example
. . .
IF v_start > 100 THEN
v_start := 2 * v_start;
ELSIF v_start >= 50 THEN
v_start := .5 * v_start;
ELSE
v_start := .1 * v_start;
END IF;
. . .
19-10 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Building Logical Conditions
• You can handle null values with the IS
NULL operator.
• Any arithmetic expression containing a
null value evaluates to NULL.
• Concatenated expressions with null
values treat null values as an empty
string.
19-11 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Logic Tables
Build a simple Boolean condition with a
comparison operator.
NOT
TRUE
FALSE
NULL
OR
TRUE
FALSE
NULL
TRUE FALSE NULL
FALSE
TRUE
NULL
AND
TRUE
FALSE
NULL
TRUE FALSE NULL
TRUE
NULL NULL
NULL
FALSE FALSE
FALSE
FALSE
FALSE
TRUE
TRUE
TRUE
TRUE
TRUE
FALSE
NULL NULL
NULL
19-12 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Boolean Conditions
What is the value of V_FLAG in each case?
V_REORDER_FLAG V_AVAILABLE_FLAG V_FLAG
TRUE TRUE
TRUE FALSE
NULL TRUE
NULL FALSE
v_flag := v_reorder_flag AND v_available_flag;
TRUE
FALSE
NULL
FALSE
19-13 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Iterative Control: LOOP
Statements
• Loops repeat a statement or sequence
of statements multiple times.
• There are three loop types:
– Basic loop
– FOR loop
– WHILE loop
19-14 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Basic Loop
Syntax
LOOP
statement1;
. . .
EXIT [WHEN condition];
END LOOP;
where: condition is a Boolean variable or
expression (TRUE, FALSE,
or NULL);
-- delimiter
-- statements
-- EXIT statement
-- delimiter
19-15 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Basic Loop
DECLARE
v_ordid item.ordid%TYPE := 601;
v_counter NUMBER(2) := 1;
BEGIN
LOOP
INSERT INTO item(ordid, itemid)
VALUES(v_ordid, v_counter);
v_counter := v_counter + 1;
EXIT WHEN v_counter > 10;
END LOOP;
END;
Example
19-16 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
FOR Loop
Syntax
• Use a FOR loop to shortcut the test for
the number of iterations.
• Do not declare the counter; it is
declared implicitly.
FOR counter in [REVERSE]
lower_bound..upper_bound LOOP
statement1;
statement2;
. . .
END LOOP;
19-17 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
FOR Loop
Guidelines
• Reference the counter within the loop
only; it is undefined outside the loop.
• Use an expression to reference the
existing value of a counter.
• Do not reference the counter as the target
of an assignment.
19-18 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
FOR Loop
Insert the first 10 new line items for order
number 601.
Example
DECLARE
v_ordid item.ordid%TYPE := 601;
BEGIN
FOR i IN 1..10 LOOP
INSERT INTO item(ordid, itemid)
VALUES(v_ordid, i);
END LOOP;
END;
19-19 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
WHILE Loop
Syntax
Use the WHILE loop to repeat statements
while a condition is TRUE.
WHILE condition LOOP
statement1;
statement2;
. . .
END LOOP;
Condition is
evaluated at the
beginning of
each iteration.
19-20 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
WHILE Loop
Example
ACCEPT p_new_order PROMPT 'Enter the order number: '
ACCEPT p_items -
PROMPT 'Enter the number of items in this order: '
DECLARE
v_count NUMBER(2) := 1;
BEGIN
WHILE v_count <= &p_items LOOP
INSERT INTO item (ordid, itemid)
VALUES (&p_new_order, v_count);
v_count := v_count + 1;
END LOOP;
COMMIT;
END;
/
19-21 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Nested Loops and Labels
• Nest loops to multiple levels.
• Use labels to distinguish between
blocks and loops.
• Exit the outer loop with the EXIT
statement referencing the label.
19-22 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Nested Loops and Labels
...
BEGIN
<<Outer_loop>>
LOOP
v_counter := v_counter+1;
EXIT WHEN v_counter>10;
<<Inner_loop>>
LOOP
...
EXIT Outer_loop WHEN total_done = 'YES';
-- Leave both loops
EXIT WHEN inner_done = 'YES';
-- Leave inner loop only
...
END LOOP Inner_loop;
...
END LOOP Outer_loop;
END;
19-23 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Change the logical flow of statements by
using control structures.
• Conditional (IF statement)
• Loops:
– Basic loop
– FOR loop
– WHILE loop
– EXIT statement
Summary
19-24 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Practice Overview
• Performing conditional actions using
the IF statement
• Performing iterative steps using the
loop structure

More Related Content

What's hot

Les02[1]Restricting and Sorting Data
Les02[1]Restricting and Sorting DataLes02[1]Restricting and Sorting Data
Les02[1]Restricting and Sorting Data
siavosh kaviani
 
Les20[1]Working with Composite Datatypes
Les20[1]Working with Composite DatatypesLes20[1]Working with Composite Datatypes
Les20[1]Working with Composite Datatypes
siavosh kaviani
 
Les01[1]Writing Basic SQL Statements
Les01[1]Writing Basic SQL StatementsLes01[1]Writing Basic SQL Statements
Les01[1]Writing Basic SQL Statements
siavosh kaviani
 
Les03[1] Single-Row Functions
Les03[1] Single-Row FunctionsLes03[1] Single-Row Functions
Les03[1] Single-Row Functions
siavosh kaviani
 
Les13[1]Other Database Objects
Les13[1]Other Database ObjectsLes13[1]Other Database Objects
Les13[1]Other Database Objects
siavosh kaviani
 
Les06[1]Subqueries
Les06[1]SubqueriesLes06[1]Subqueries
Les06[1]Subqueries
siavosh kaviani
 
Les09[1]Manipulating Data
Les09[1]Manipulating DataLes09[1]Manipulating Data
Les09[1]Manipulating Data
siavosh kaviani
 
Les10[1]Creating and Managing Tables
Les10[1]Creating and Managing TablesLes10[1]Creating and Managing Tables
Les10[1]Creating and Managing Tables
siavosh kaviani
 
Les05[1]Aggregating Data Using Group Functions
Les05[1]Aggregating Data  Using Group FunctionsLes05[1]Aggregating Data  Using Group Functions
Les05[1]Aggregating Data Using Group Functions
siavosh kaviani
 
Les12[1]Creating Views
Les12[1]Creating ViewsLes12[1]Creating Views
Les12[1]Creating Views
siavosh kaviani
 
What is Listagg?
What is Listagg?What is Listagg?
What is Listagg?Kai Liu
 
Single row functions
Single row functionsSingle row functions
Single row functions
Balqees Al.Mubarak
 
Sql functions
Sql functionsSql functions
Sql functions
Ankit Dubey
 
PLSQL Advanced
PLSQL AdvancedPLSQL Advanced
PLSQL Advanced
Quang Minh Đoàn
 
Beg sql
Beg sqlBeg sql
Oracle: Control Structures
Oracle: Control StructuresOracle: Control Structures
Oracle: Control Structures
DataminingTools Inc
 
PL-SQL DIFFERENT PROGRAMS
PL-SQL DIFFERENT PROGRAMSPL-SQL DIFFERENT PROGRAMS
PL-SQL DIFFERENT PROGRAMS
raj upadhyay
 
Oracle views
Oracle viewsOracle views
Oracle views
Balqees Al.Mubarak
 

What's hot (20)

Les02[1]Restricting and Sorting Data
Les02[1]Restricting and Sorting DataLes02[1]Restricting and Sorting Data
Les02[1]Restricting and Sorting Data
 
Les20[1]Working with Composite Datatypes
Les20[1]Working with Composite DatatypesLes20[1]Working with Composite Datatypes
Les20[1]Working with Composite Datatypes
 
Les01[1]Writing Basic SQL Statements
Les01[1]Writing Basic SQL StatementsLes01[1]Writing Basic SQL Statements
Les01[1]Writing Basic SQL Statements
 
Les03[1] Single-Row Functions
Les03[1] Single-Row FunctionsLes03[1] Single-Row Functions
Les03[1] Single-Row Functions
 
Les13[1]Other Database Objects
Les13[1]Other Database ObjectsLes13[1]Other Database Objects
Les13[1]Other Database Objects
 
Les06[1]Subqueries
Les06[1]SubqueriesLes06[1]Subqueries
Les06[1]Subqueries
 
Les09[1]Manipulating Data
Les09[1]Manipulating DataLes09[1]Manipulating Data
Les09[1]Manipulating Data
 
Les10[1]Creating and Managing Tables
Les10[1]Creating and Managing TablesLes10[1]Creating and Managing Tables
Les10[1]Creating and Managing Tables
 
Les05[1]Aggregating Data Using Group Functions
Les05[1]Aggregating Data  Using Group FunctionsLes05[1]Aggregating Data  Using Group Functions
Les05[1]Aggregating Data Using Group Functions
 
Les12[1]Creating Views
Les12[1]Creating ViewsLes12[1]Creating Views
Les12[1]Creating Views
 
What is Listagg?
What is Listagg?What is Listagg?
What is Listagg?
 
Plsql
PlsqlPlsql
Plsql
 
Single row functions
Single row functionsSingle row functions
Single row functions
 
Sql functions
Sql functionsSql functions
Sql functions
 
PLSQL Advanced
PLSQL AdvancedPLSQL Advanced
PLSQL Advanced
 
Beg sql
Beg sqlBeg sql
Beg sql
 
Beg sql
Beg sqlBeg sql
Beg sql
 
Oracle: Control Structures
Oracle: Control StructuresOracle: Control Structures
Oracle: Control Structures
 
PL-SQL DIFFERENT PROGRAMS
PL-SQL DIFFERENT PROGRAMSPL-SQL DIFFERENT PROGRAMS
PL-SQL DIFFERENT PROGRAMS
 
Oracle views
Oracle viewsOracle views
Oracle views
 

Similar to Les19[1]Writing Control Structures

Oracle: Control Structures
Oracle:  Control StructuresOracle:  Control Structures
Oracle: Control Structures
oracle content
 
PL/SQL CONDICIONALES Y CICLOS
PL/SQL CONDICIONALES Y CICLOSPL/SQL CONDICIONALES Y CICLOS
PL/SQL CONDICIONALES Y CICLOS
Richard Eliseo Mendoza Gafaro
 
Oracle - Program with PL/SQL - Lession 04
Oracle - Program with PL/SQL - Lession 04Oracle - Program with PL/SQL - Lession 04
Oracle - Program with PL/SQL - Lession 04
Thuan Nguyen
 
PLSQL (1).ppt
PLSQL (1).pptPLSQL (1).ppt
PLSQL (1).ppt
MadhuriAnaparthy
 
Kotlin For Beginners - CheezyCode
Kotlin For Beginners - CheezyCodeKotlin For Beginners - CheezyCode
Kotlin For Beginners - CheezyCode
Cheezy Code
 
Presentation on nesting of loops
Presentation on nesting of loopsPresentation on nesting of loops
Presentation on nesting of loopsbsdeol28
 
PL/SQL CURSORES
PL/SQL CURSORESPL/SQL CURSORES
ORACLE PL/SQL
ORACLE PL/SQLORACLE PL/SQL
ORACLE PL/SQL
ASHABOOPATHY
 
5.pptx fundamental programing one branch
5.pptx fundamental programing one branch5.pptx fundamental programing one branch
5.pptx fundamental programing one branch
ssuserdde43b
 
Web Application Development using PHP Chapter 2
Web Application Development using PHP Chapter 2Web Application Development using PHP Chapter 2
Web Application Development using PHP Chapter 2
Mohd Harris Ahmad Jaal
 
Loops c++
Loops c++Loops c++
Loops c++
Shivani Singh
 
Loop structures
Loop structuresLoop structures
Loop structures
tazeem sana
 
Looping in c++
Looping in c++Looping in c++
Looping in c++
deekshagopaliya
 
Looping in c++
Looping in c++Looping in c++
Looping in c++
deekshagopaliya
 
Pl sql chapter 2
Pl sql chapter 2Pl sql chapter 2
Pl sql chapter 2
PrabhatKumar591
 
Pseudocode
PseudocodePseudocode
Pseudocode
Harsha Madushanka
 
Deeksha gopaliya
Deeksha gopaliyaDeeksha gopaliya
Deeksha gopaliya
deekshagopaliya
 
PL/SQL Part 5
PL/SQL Part 5PL/SQL Part 5
PL/SQL Part 5
Gurpreet singh
 

Similar to Les19[1]Writing Control Structures (20)

Oracle: Control Structures
Oracle:  Control StructuresOracle:  Control Structures
Oracle: Control Structures
 
PL/SQL CONDICIONALES Y CICLOS
PL/SQL CONDICIONALES Y CICLOSPL/SQL CONDICIONALES Y CICLOS
PL/SQL CONDICIONALES Y CICLOS
 
Oracle - Program with PL/SQL - Lession 04
Oracle - Program with PL/SQL - Lession 04Oracle - Program with PL/SQL - Lession 04
Oracle - Program with PL/SQL - Lession 04
 
PLSQL (1).ppt
PLSQL (1).pptPLSQL (1).ppt
PLSQL (1).ppt
 
Kotlin For Beginners - CheezyCode
Kotlin For Beginners - CheezyCodeKotlin For Beginners - CheezyCode
Kotlin For Beginners - CheezyCode
 
Presentation on nesting of loops
Presentation on nesting of loopsPresentation on nesting of loops
Presentation on nesting of loops
 
PL/SQL CURSORES
PL/SQL CURSORESPL/SQL CURSORES
PL/SQL CURSORES
 
ORACLE PL/SQL
ORACLE PL/SQLORACLE PL/SQL
ORACLE PL/SQL
 
Stored procedure
Stored procedureStored procedure
Stored procedure
 
Stored procedure
Stored procedureStored procedure
Stored procedure
 
5.pptx fundamental programing one branch
5.pptx fundamental programing one branch5.pptx fundamental programing one branch
5.pptx fundamental programing one branch
 
Web Application Development using PHP Chapter 2
Web Application Development using PHP Chapter 2Web Application Development using PHP Chapter 2
Web Application Development using PHP Chapter 2
 
Loops c++
Loops c++Loops c++
Loops c++
 
Loop structures
Loop structuresLoop structures
Loop structures
 
Looping in c++
Looping in c++Looping in c++
Looping in c++
 
Looping in c++
Looping in c++Looping in c++
Looping in c++
 
Pl sql chapter 2
Pl sql chapter 2Pl sql chapter 2
Pl sql chapter 2
 
Pseudocode
PseudocodePseudocode
Pseudocode
 
Deeksha gopaliya
Deeksha gopaliyaDeeksha gopaliya
Deeksha gopaliya
 
PL/SQL Part 5
PL/SQL Part 5PL/SQL Part 5
PL/SQL Part 5
 

More from siavosh kaviani

sara-shortCV SARA GHIASI TABRIZI Computer Science PhD Application
sara-shortCV SARA GHIASI TABRIZI Computer Science PhD Applicationsara-shortCV SARA GHIASI TABRIZI Computer Science PhD Application
sara-shortCV SARA GHIASI TABRIZI Computer Science PhD Application
siavosh kaviani
 
Introduction-to-the-Lean-Canvas.pdf
Introduction-to-the-Lean-Canvas.pdfIntroduction-to-the-Lean-Canvas.pdf
Introduction-to-the-Lean-Canvas.pdf
siavosh kaviani
 
Attaque chimique contre les écolières en Iran version 2.pptx
Attaque chimique contre les écolières en Iran version 2.pptxAttaque chimique contre les écolières en Iran version 2.pptx
Attaque chimique contre les écolières en Iran version 2.pptx
siavosh kaviani
 
Short CV BA.pdf
Short CV BA.pdfShort CV BA.pdf
Short CV BA.pdf
siavosh kaviani
 
Faegh Omidi Resume.pdf
Faegh Omidi Resume.pdfFaegh Omidi Resume.pdf
Faegh Omidi Resume.pdf
siavosh kaviani
 
Short CV CTO version 2.pdf
Short CV CTO version 2.pdfShort CV CTO version 2.pdf
Short CV CTO version 2.pdf
siavosh kaviani
 
Short CV Marketing version 2.pdf
Short CV Marketing version 2.pdfShort CV Marketing version 2.pdf
Short CV Marketing version 2.pdf
siavosh kaviani
 
Short CV prof version 2.pdf
Short CV prof version 2.pdfShort CV prof version 2.pdf
Short CV prof version 2.pdf
siavosh kaviani
 
Siavosh Kaviani cv francais 2022 version 2.pdf
Siavosh Kaviani cv francais 2022 version 2.pdfSiavosh Kaviani cv francais 2022 version 2.pdf
Siavosh Kaviani cv francais 2022 version 2.pdf
siavosh kaviani
 
SiavoshKaviani-CV[2021] francais.pdf
SiavoshKaviani-CV[2021] francais.pdfSiavoshKaviani-CV[2021] francais.pdf
SiavoshKaviani-CV[2021] francais.pdf
siavosh kaviani
 
apex security demo.ppsx
apex security demo.ppsxapex security demo.ppsx
apex security demo.ppsx
siavosh kaviani
 
Les14[1]Controlling User Access
Les14[1]Controlling User AccessLes14[1]Controlling User Access
Les14[1]Controlling User Access
siavosh kaviani
 
Les15[1]SQL Workshop
Les15[1]SQL WorkshopLes15[1]SQL Workshop
Les15[1]SQL Workshop
siavosh kaviani
 
Les11[1]Including Constraints
Les11[1]Including ConstraintsLes11[1]Including Constraints
Les11[1]Including Constraints
siavosh kaviani
 

More from siavosh kaviani (14)

sara-shortCV SARA GHIASI TABRIZI Computer Science PhD Application
sara-shortCV SARA GHIASI TABRIZI Computer Science PhD Applicationsara-shortCV SARA GHIASI TABRIZI Computer Science PhD Application
sara-shortCV SARA GHIASI TABRIZI Computer Science PhD Application
 
Introduction-to-the-Lean-Canvas.pdf
Introduction-to-the-Lean-Canvas.pdfIntroduction-to-the-Lean-Canvas.pdf
Introduction-to-the-Lean-Canvas.pdf
 
Attaque chimique contre les écolières en Iran version 2.pptx
Attaque chimique contre les écolières en Iran version 2.pptxAttaque chimique contre les écolières en Iran version 2.pptx
Attaque chimique contre les écolières en Iran version 2.pptx
 
Short CV BA.pdf
Short CV BA.pdfShort CV BA.pdf
Short CV BA.pdf
 
Faegh Omidi Resume.pdf
Faegh Omidi Resume.pdfFaegh Omidi Resume.pdf
Faegh Omidi Resume.pdf
 
Short CV CTO version 2.pdf
Short CV CTO version 2.pdfShort CV CTO version 2.pdf
Short CV CTO version 2.pdf
 
Short CV Marketing version 2.pdf
Short CV Marketing version 2.pdfShort CV Marketing version 2.pdf
Short CV Marketing version 2.pdf
 
Short CV prof version 2.pdf
Short CV prof version 2.pdfShort CV prof version 2.pdf
Short CV prof version 2.pdf
 
Siavosh Kaviani cv francais 2022 version 2.pdf
Siavosh Kaviani cv francais 2022 version 2.pdfSiavosh Kaviani cv francais 2022 version 2.pdf
Siavosh Kaviani cv francais 2022 version 2.pdf
 
SiavoshKaviani-CV[2021] francais.pdf
SiavoshKaviani-CV[2021] francais.pdfSiavoshKaviani-CV[2021] francais.pdf
SiavoshKaviani-CV[2021] francais.pdf
 
apex security demo.ppsx
apex security demo.ppsxapex security demo.ppsx
apex security demo.ppsx
 
Les14[1]Controlling User Access
Les14[1]Controlling User AccessLes14[1]Controlling User Access
Les14[1]Controlling User Access
 
Les15[1]SQL Workshop
Les15[1]SQL WorkshopLes15[1]SQL Workshop
Les15[1]SQL Workshop
 
Les11[1]Including Constraints
Les11[1]Including ConstraintsLes11[1]Including Constraints
Les11[1]Including Constraints
 

Recently uploaded

Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
RaedMohamed3
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
vaibhavrinwa19
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Po-Chuan Chen
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
Levi Shapiro
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Atul Kumar Singh
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
DhatriParmar
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
Jheel Barad
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 

Recently uploaded (20)

Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 

Les19[1]Writing Control Structures

  • 1. Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. 19 Writing Control Structures
  • 2. 19-2 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Objectives After completing this lesson, you should be able to do the following: • Identify the uses and types of control structures • Construct an IF statement • Construct and identify different loop statements • Use logic tables • Control block flow using nested loops and labels
  • 3. 19-3 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Controlling PL/SQL Flow of Execution You can change the logical flow of statements using conditional IF statements and loop control structures. Conditional IF statements: • IF-THEN-END IF • IF-THEN-ELSE-END IF • IF-THEN-ELSIF-END IF
  • 4. 19-4 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. IF Statements IF condition THEN statements; [ELSIF condition THEN statements;] [ELSE statements;] END IF; Syntax Simple IF statement: Set the manager ID to 22 if the employee name is Osborne. IF v_ename = 'OSBORNE' THEN v_mgr := 22; END IF;
  • 5. 19-5 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Simple IF Statements Set the job title to Salesman, the department number to 35, and the commission to 20% of the current salary if the last name is Miller. Example . . . IF v_ename = 'MILLER' THEN v_job := 'SALESMAN'; v_deptno := 35; v_new_comm := sal * 0.20; END IF; . . .
  • 6. 19-6 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. IF-THEN-ELSE Statement Execution Flow IF condition TRUE THEN actions (including further IFs) FALSE ELSE actions (including further IFs)
  • 7. 19-7 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. IF-THEN-ELSE Statements Set a flag for orders where there are fewer than five days between order date and ship date. Example ... IF v_shipdate - v_orderdate < 5 THEN v_ship_flag := 'Acceptable'; ELSE v_ship_flag := 'Unacceptable'; END IF; ...
  • 8. 19-8 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. IF-THEN-ELSIF Statement Execution Flow IF condition TRUE THEN actions FALSE ELSIF condition TRUE THEN actions FALSE ELSE actions
  • 9. 19-9 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. IF-THEN-ELSIF Statements For a given value, calculate a percentage of that value based on a condition. Example . . . IF v_start > 100 THEN v_start := 2 * v_start; ELSIF v_start >= 50 THEN v_start := .5 * v_start; ELSE v_start := .1 * v_start; END IF; . . .
  • 10. 19-10 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Building Logical Conditions • You can handle null values with the IS NULL operator. • Any arithmetic expression containing a null value evaluates to NULL. • Concatenated expressions with null values treat null values as an empty string.
  • 11. 19-11 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Logic Tables Build a simple Boolean condition with a comparison operator. NOT TRUE FALSE NULL OR TRUE FALSE NULL TRUE FALSE NULL FALSE TRUE NULL AND TRUE FALSE NULL TRUE FALSE NULL TRUE NULL NULL NULL FALSE FALSE FALSE FALSE FALSE TRUE TRUE TRUE TRUE TRUE FALSE NULL NULL NULL
  • 12. 19-12 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Boolean Conditions What is the value of V_FLAG in each case? V_REORDER_FLAG V_AVAILABLE_FLAG V_FLAG TRUE TRUE TRUE FALSE NULL TRUE NULL FALSE v_flag := v_reorder_flag AND v_available_flag; TRUE FALSE NULL FALSE
  • 13. 19-13 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Iterative Control: LOOP Statements • Loops repeat a statement or sequence of statements multiple times. • There are three loop types: – Basic loop – FOR loop – WHILE loop
  • 14. 19-14 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Basic Loop Syntax LOOP statement1; . . . EXIT [WHEN condition]; END LOOP; where: condition is a Boolean variable or expression (TRUE, FALSE, or NULL); -- delimiter -- statements -- EXIT statement -- delimiter
  • 15. 19-15 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Basic Loop DECLARE v_ordid item.ordid%TYPE := 601; v_counter NUMBER(2) := 1; BEGIN LOOP INSERT INTO item(ordid, itemid) VALUES(v_ordid, v_counter); v_counter := v_counter + 1; EXIT WHEN v_counter > 10; END LOOP; END; Example
  • 16. 19-16 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. FOR Loop Syntax • Use a FOR loop to shortcut the test for the number of iterations. • Do not declare the counter; it is declared implicitly. FOR counter in [REVERSE] lower_bound..upper_bound LOOP statement1; statement2; . . . END LOOP;
  • 17. 19-17 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. FOR Loop Guidelines • Reference the counter within the loop only; it is undefined outside the loop. • Use an expression to reference the existing value of a counter. • Do not reference the counter as the target of an assignment.
  • 18. 19-18 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. FOR Loop Insert the first 10 new line items for order number 601. Example DECLARE v_ordid item.ordid%TYPE := 601; BEGIN FOR i IN 1..10 LOOP INSERT INTO item(ordid, itemid) VALUES(v_ordid, i); END LOOP; END;
  • 19. 19-19 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. WHILE Loop Syntax Use the WHILE loop to repeat statements while a condition is TRUE. WHILE condition LOOP statement1; statement2; . . . END LOOP; Condition is evaluated at the beginning of each iteration.
  • 20. 19-20 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. WHILE Loop Example ACCEPT p_new_order PROMPT 'Enter the order number: ' ACCEPT p_items - PROMPT 'Enter the number of items in this order: ' DECLARE v_count NUMBER(2) := 1; BEGIN WHILE v_count <= &p_items LOOP INSERT INTO item (ordid, itemid) VALUES (&p_new_order, v_count); v_count := v_count + 1; END LOOP; COMMIT; END; /
  • 21. 19-21 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Nested Loops and Labels • Nest loops to multiple levels. • Use labels to distinguish between blocks and loops. • Exit the outer loop with the EXIT statement referencing the label.
  • 22. 19-22 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Nested Loops and Labels ... BEGIN <<Outer_loop>> LOOP v_counter := v_counter+1; EXIT WHEN v_counter>10; <<Inner_loop>> LOOP ... EXIT Outer_loop WHEN total_done = 'YES'; -- Leave both loops EXIT WHEN inner_done = 'YES'; -- Leave inner loop only ... END LOOP Inner_loop; ... END LOOP Outer_loop; END;
  • 23. 19-23 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Change the logical flow of statements by using control structures. • Conditional (IF statement) • Loops: – Basic loop – FOR loop – WHILE loop – EXIT statement Summary
  • 24. 19-24 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Practice Overview • Performing conditional actions using the IF statement • Performing iterative steps using the loop structure