Program with PL/SQL Lesson 4.

Slides:



Advertisements
Similar presentations
PHP Functions and Control Structures. 2 Defining Functions Functions are groups of statements that you can execute as a single unit Function definitions.
Advertisements

Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
Chapter 4B: More Advanced PL/SQL Programming
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Introduction to PL/SQL
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 2 Basic PL/SQL Block Structures.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
DiagrammaticRepresentation Iteration Construct False True Condition Exit from Statement (s) loop Sequence construct Selection construct Statement 1 Statement.
PL/SQL Loops. Building Logical Conditions All logical conditions must yield a boolean condition. You can build a simple Boolean condition by combining.
Meet Perl, Part 2 Flow of Control and I/O. Perl Statements Lots of different ways to write similar statements –Can make your code look more like natural.
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.
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.
PHP Programming with MySQL Slide 4-1 CHAPTER 4 Functions and Control Structures.
Program with PL/SQL Lesson 4. Writing Control Structure.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
PL/SQL. Introduction to PL/SQL block Declare declarations Begin executable statement Exception exception handlers End;
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
Bordoloi and Bock Control Structures: Iterative Control.
WRITING CONTROL STRUCTURES (CONDITIONAL CONTROL).
Copyright © 2004, Oracle. All rights reserved. Lecture 4: 1-Retrieving Data Using the SQL SELECT Statement 2-Restricting and Sorting Data Lecture 4: 1-Retrieving.
JavaScript, Fourth Edition
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.
Introduction to Explicit Cursors. 2 home back first prev next last What Will I Learn? Distinguish between an implicit and an explicit cursor Describe.
Retrieving Data in PL/SQL. 2 home back first prev next last What Will I Learn? In this lesson, you will learn to: –Recognize the SQL statements that can.
implicit and an explicit cursor
6 Copyright © 2007, Oracle. All rights reserved. Retrieving Data Using Subqueries.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Program with PL/SQL Lesson 3. Interacting with the Oracle Server.
6 Copyright © 2006, Oracle. All rights reserved. Retrieving Data Using Subqueries.
7 Copyright © 2004, Oracle. All rights reserved. Using Explicit Cursors.
Using Subqueries to Solve Queries
PL/SQL CURSORS.
CS322: Database Systems PL/ SQL PL/SQL by Ivan Bayross.
Chapter 6: Loops.
Interacting with the Oracle Server
Creating Stored Procedures and Functions
Writing Control Structures
Interacting with the Oracle Server
Oracle11g: PL/SQL Programming Chapter 2 Basic PL/SQL Block Structures.
Writing Control Structures
Using the Set Operators
Topics The if Statement The if-else Statement Comparing Strings
JavaScript: Control Statements.
Using Subqueries to Solve Queries
Database Management Systems 2
Writing Correlated Subqueries
Database Management Systems 2
Database Management Systems 2
Arrays, For loop While loop Do while loop
Topics The if Statement The if-else Statement Comparing Strings
Additional Control Structures
Writing Control Structures
Control Structures: for & while Loops
Cursors.
Using Subqueries to Solve Queries
Computer Science Core Concepts
Chapter 4: Control Structures I (Selection)
Chapter 8 Advanced SQL.
PL/SQL Declaring Variables.
Information Management
Using Subqueries to Solve Queries
Using Subqueries to Solve Queries
Using the Set Operators
Subqueries Schedule: Timing Topic 25 minutes Lecture
CSC215 Lecture Control Flow.
Database Programming Using Oracle 11g
Presentation transcript:

Program with PL/SQL Lesson 4

Writing Control Structure

Controlling PL/SQL Flow of Execution Change the logical execution 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

IF Statements Syntax: IF condition THEN statements; [ELSIF condition THEN statements;] [ELSE END IF;

CASE Expressions A CASE expressions selects a result and returns it To select the result. The CASE expressions uses an expression whose values is used to select one of several alternatives CASE selector WHEN expressions1 THEN result1 WHEN expressions2 THEN result2 … WHEN expressionsN THEN resultN [ELSE resultN+1;] END;

Handling Nulls Simple comparison involving nulls always yield NULL Applying the logical operator NOT to a null yields NULL In conditional control statements, if the condition yields NULL, its associated sequence of statements is not executed

Logic Tables AND TRUE FALSE NULL OR TRUE FALSE NULL

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

Basic Loops Syntax: LOOP -- delimiter Statement1; -- statements ... EXIT [WHEN condition]; -- EXIT statements END LOOP -- delimiter

WHILE Loops Syntax: WHILE condition LOOP Statement1; Statement2; ... END LOOP Condition is evaluated at the beginning of each iteration Use the WHILE loop to repeat statements while a condition is TRUE

FOR Loops Syntax: FOR counter IN [REVERSE] lower_bound .. Upper_bound LOOP Statement1; Statement2; ... END LOOP Use a FOR loop to shortcut the test for the number of iterations Do not declare the counter; it is declared implicitly ‘lower_bound .. Upper_bound’ is required syntax

FOR Loops Guidelines Reference the counter within the loop only; it is undefined outside the loop Do not reference the counter as the targert of an assignment

Guidelines While Using Loops Use the basic loop when the statements inside the loop must execute at least once Use the WHILE loop if the condition has to be evaluated at the start of each iteration Use a FOR loop if the number of iterations is known

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 that references the label

Practice Overview

Practice 1 Create a PL/SQL block that rewards an employee by appending an asterisk in the STARS column for every $1000 of the employee’s salary. Use the DEFINE command to provide the employee_id. Pass the value to the PL/SQL block through a iSQL*Plus substitution variable Initialize a v_asterisk variable that contains a NULL Append an asterisk to the string for every $1000 of the salary amount. For example, if the employee has a salary amount $8000, the string of asterisk should contain eight asterisk. If the employee has a salary amount of $12500, the string of asterisks should contains 13 asterisks Update the STARS column for the employee with the string of asterisk COMMIT

Practice 2 In a loop, use a cursor to retrieve the department number and department name from the DEPARTMENTS table for those departments whose DEPARTMENT_ID is less than 100. Pass the department number to another cursor to retrieve from the EMPLOYEES table the details of employee last name, job, hire_date, and salary of those employees whose EMPLOYEE_ID is less than 120 and who work in that department.

Practice 3 In a loop use the iSQL*Plus substitution parameter created in step 1 and gather the salaries of the top n people from the EMPLOYEES table. There should be no duplication in the salaries. If two employees earn the same salary, the salary should be picked up only once and store the salaries in the TOP_DOGS table