Oracle11g: PL/SQL Programming Chapter 2 Basic PL/SQL Block Structures.

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

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.
PL/SQL.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 4 Making Decisions in a Program.
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
Decisions (Conditional Programming) Chapter 5 (Sec. 5.1 & 5.2)
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
PL/SQL The Oracle Programming Language. Purpose SQL is a non-procedural language. Often we need to put structure on transactions, so we use the supplemental.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
Introduction to PL/SQL
Introduction to PL/SQL. Procedural Language extension for SQL Oracle Proprietary 3GL Capabilities Integration of SQL Portable within Oracle data bases.
Oracle10g Developer: PL/SQL Programming1 Objectives Manipulating data with cursors Managing errors with exception handlers Addressing exception-handling.
Chapter 4 Cursors and Exception Handling Oracle10g Developer:
Oracle10g Developer: PL/SQL Programming1 Objectives Programming fundamentals The PL/SQL block Define and declare variables Initialize variables The NOT.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 2 Basic PL/SQL Block Structures.
INTRODUCTION TO PL/SQL. Class Agenda Introduction Introduction to PL/SQL Declaring PL/SQL Variable Creating the Executable Section Interacting with the.
1 Copyright © 2004, Oracle. All rights reserved. Introduction to PL/SQL.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 6 Functions.
PL/SQLPL/SQL Oracle11g : PL/SQL Programming Chapter 6 Functions.
Overview · What is PL/SQL · Advantages of PL/SQL · Basic Structure of a PL/SQL Block · Procedure · Function · Anonymous Block · Types of Block · Declaring.
Program with PL/SQL Lesson 4. Writing Control Structure.
PL / SQL By Mohammed Baihan. What is PL/SQL? PL/SQL stands for Procedural Language extension of SQL. PL/SQL is a combination of SQL along with the procedural.
Script Writing. Objectives: Definition of script writing Types of script Creating/Modification and running script Execution of script Fundamental of Script.
1 Copyright © 2004, Oracle. All rights reserved. Introduction to PL/SQL.
CIS4368: Advanced DatabaseSlide # 1 PL/SQL Dr. Peeter KirsSpring, 2003 PL/SQL.
PL/SQL Declaring Variables PL/SQL Block Structure DECLARE (Optional) Variables, cursors, user-defined exceptions BEGIN (Mandatory) - SQL statements -
Chapter 15 Introduction to PL/SQL. Chapter Objectives  Explain the benefits of using PL/SQL blocks versus several SQL statements  Identify the sections.
Database Application Development using PL/SQL Programming.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 3 Handling Data in PL/SQL Blocks.
1 PL/SQLPL/SQL Declaring Variables Declaring Variables Declaring Variables Declaring Variables Writing Executable Statements Writing Executable Statements.
Copyright  Oracle Corporation, All rights reserved. 16 Declaring Variables.
WRITING CONTROL STRUCTURES (CONDITIONAL CONTROL).
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 9 Database Triggers.
PL/SQLPL/SQL Oracle11g: PL/SQL Programming Chapter 4 Cursors and Exception Handling.
Chapter 16 Cursors and Exceptions. Chapter Objectives  Determine when an explicit cursor is required  Declare, open, and close an explicit cursor 
Oracle10g Developer: PL/SQL Programming1 Objectives SQL queries within PL/SQL Host or bind variables The %TYPE attribute Include queries and control structures.
PL/SQLPL/SQL Oracle11g: PL/SQL Programming Chapter 9 Database Triggers.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 9 Database Triggers.
Oracle11g: PL/SQL Programming Chapter 3 Handling Data in PL/SQL Blocks.
Oracle10g Developer: PL/SQL Programming1 Objectives Named program units How to identify parameters The CREATE PROCEDURE statement Creating a procedure.
Lab 2 Writing PL/SQL Blocks CISB514 Advanced Database Systems.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Introduction to PL/SQL N. Dimililer. About PL/SQL –PL/SQL is an extension to SQL with design features of programming languages. –Data manipulation and.
Control Structure  What is control Structure?  Types of Controls  Use the control structure in VBScript.  Example Summery.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
Oracle11g: PL/SQL Programming Chapter 5 Procedures.
Program with PL/SQL Lesson 4.
Writing Control Structures
Fundamentals of PL/SQL part 1 (Basics)
Oracle11g: PL/SQL Programming Chapter 4 Cursors and Exception Handling.
Chapter 6 More Conditionals and Loops
Oracle9i Developer: PL/SQL Programming Chapter 3 PL/SQL Processing.
Making Decisions in a Program
Outline Altering flow of control Boolean expressions
Sa0951a PL/SQL 1: Introduction
Chapter 4: Introduction to PL/SQL
Chapter 2 Handling Data in PL/SQL Blocks Oracle9i Developer:
Objectives After studying this chapter, you should be able to:
Chapter 8: More on the Repetition Structure
Microsoft Visual Basic 2005: Reloaded Second Edition
PL/SQL Declaring Variables.
Decisions, decisions, decisions
Prof. Arfaoui. COM390 Chapter 9
Procedures Oracle & MySQL
Prof. Arfaoui. COM390 Chapter 6
Handling Data in PL/SQL Blocks
The Selection Structure
Prof. Arfaoui. COM390 Chapter 7
Presentation transcript:

Oracle11g: PL/SQL Programming Chapter 2 Basic PL/SQL Block Structures

Chapter Objectives After completing this lesson, you should be able to understand: Programming fundamentals PL/SQL blocks How to define and declare variables How to initialize and manage variable values The NOT NULL and CONSTANT variable options Oracle11g: PL/SQL Programming

Chapter Objectives (continued) After completing this lesson, you should be able to understand (continued): How to perform calculations with variables The use of SQL single-row functions in PL/SQL statements Decision structures: IF-THEN and CASE Looping actions: basic, FOR and WHILE CONTINUE statements Nested Statements Oracle11g: PL/SQL Programming

Program Logic Flow Identify sequence of actions needed prior to coding Use a flowchart to visually represent the sequence of actions Oracle11g: PL/SQL Programming

Flowcharting - Search for Coffee Products Oracle11g: PL/SQL Programming

Decision Structures Oracle11g: PL/SQL Programming

Looping Structures Oracle11g: PL/SQL Programming

PL/SQL Block Questions What is a block? What are the different segments of a block? How does data get into a block? How are different data types handled? Oracle11g: PL/SQL Programming

Brewbean’s Challenge Oracle11g: PL/SQL Programming

PL/SQL Block Structure DECLARE – create variables, cursors, and types BEGIN – SQL, logic, loops, assignment statements EXCEPTION – error handling END – close the block Oracle11g: PL/SQL Programming

Variable Names Begin with alpha character Up to 30 characters Can contain upper and lowercase letters, numbers, _ , $ , # Oracle11g: PL/SQL Programming

Scalar Variable Data Types Character – CHAR(n) VARCHAR2(n) Numeric – NUMBER(p,s) Date – DATE Boolean – BOOLEAN Note: Only holds a single value Oracle11g: PL/SQL Programming

Example Scalar Declarations DECLARE lv_ord_date DATE; lv_last_txt VARCHAR2(25); lv_qty_num NUMBER(2); lv_shipflag_bln BOOLEAN; BEGIN ---- PL/SQL executable statements ---- END;   Note: Minimum requirements are variable name and data type Oracle11g: PL/SQL Programming

Test Variables Oracle11g: PL/SQL Programming

Variable Initialization Set a variable value when the variable is created DECLARE lv_ord_date DATE := SYSDATE; lv_last_txt VARCHAR2(25) := 'Unknown'; lv_qty_num NUMBER(2) := 0; lv_shipflag_bln BOOLEAN := 'FALSE'; BEGIN ---- PL/SQL executable statements ---- END; Oracle11g: PL/SQL Programming

Test Variable Initialization Oracle11g: PL/SQL Programming

Variable Declaration Options NOT NULL – the variable must always contain a value CONSTANT – the variable value can not be changed in the block DECLARE lv_shipcntry_txt VARCHAR2(15) NOT NULL := 'US'; lv_taxrate_num CONSTANT NUMBER(2,2) := .06; BEGIN ---- PL/SQL executable statements ---- END; Oracle11g: PL/SQL Programming

Calculations with Scalar Variables multiplication DECLARE lv_taxrate_num CONSTANT NUMBER(2,2) := .06; lv_total_num NUMBER(6,2) := 50; lv_taxamt_num NUMBER(4,2); BEGIN lv_taxamt_num := lv_total_num * lv_taxrate_num; DBMS_OUTPUT.PUT_LINE(lv_taxamt_num); END; / Oracle11g: PL/SQL Programming

Using SQL Functions SQL functions such as MONTHS_BETWEEN can be used within PL/SQL statements Oracle11g: PL/SQL Programming

Decision Structures Control which statements in a PL/SQL block will execute Enables conditions to be tested to determine the flow of statement execution Most programming languages provide IF and CASE statements to enable conditional processing Oracle11g: PL/SQL Programming

Decision Structures (continued) IF Statements Simple IF IF/THEN/ELSE IF/THEN/ELSIF/ELSE CASE Statements Basic CASE statement Searched CASE statement CASE expression Oracle11g: PL/SQL Programming

Basic IF Statement Oracle11g: PL/SQL Programming

IF/THEN/ELSE Oracle11g: PL/SQL Programming

IF/THEN/ELSIF/ELSE Oracle11g: PL/SQL Programming

Nested IF IF lv_type_txt = 'E' THEN IF lv_price_num > 85 THEN <-- Inner or nested IF begins lv_disc_num = .20; ELSIF lv_price_num > 45 THEN lv_disc_num = .15; ELSE lv_disc_num = .10; END IF; <-- Inner or nested IF ends ELSIF lv_type_txt = 'C' THEN lv_disc_num = .05; END IF; Oracle11g: PL/SQL Programming

Logical Operators within IF Logical operators (AND, OR) enable multiple conditions to be checked IF lv_state_txt = 'VA' OR lv_state_txt = 'PA' THEN lv_tax_num := lv_sub_num * .06; ELSE lv_tax_num := lv_sub_num * .04; END IF; Oracle11g: PL/SQL Programming

Basic CASE Statement Oracle11g: PL/SQL Programming

Searched CASE Oracle11g: PL/SQL Programming

CASE Expression Oracle11g: PL/SQL Programming

Looping Enables a statement or set of statements to be executed more than once A loop must provide instructions of when to end the looping, or an ‘infinite’ loop will be produced Oracle11g: PL/SQL Programming

Basic LOOP Oracle11g: PL/SQL Programming

WHILE Loop Oracle11g: PL/SQL Programming

FOR Loop Oracle11g: PL/SQL Programming

CONTINUE Statement Oracle11g: PL/SQL Programming

Nested Loops Oracle11g: PL/SQL Programming

Summary A flowchart assists in laying out processing logic A PL/SQL block contains a DECLARE, BEGIN, EXCEPTION, and END sections Variables to hold values are declared Scalar variables hold a single data value Scalar variables can hold string values, numbers, dates, and Boolean values DBMS_OUTPUT.PUT_LINE is used to display values Oracle11g: PL/SQL Programming

Summary (continued) IF statement structure is IF/THEN/ELSIF/ELSE CASE statements provide decision processing similar to IF statements Looping structures include: basic, WHILE, and FOR Host or bind variables can be used to interact with the application environment Oracle11g: PL/SQL Programming