Oracle9i Developer: PL/SQL Programming Chapter 3 PL/SQL Processing.

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.
Chapter 4B: More Advanced PL/SQL Programming
Introduction to PL/SQL
Exception Handling in PL/SQL. POINTS TO DISCUSS What is Exception Handling Structure of Exception Handling Section Types of Exceptions.
PL/SQL block has the following structure: DECLARE Declaration statements BEGIN Executable statements EXCEPTION Exception-handling statements END ;
PL / SQL P rocedural L anguage / S tructured Q uery L anguage Chapter 7 in Lab Reference.
Bordoloi and Bock EXCEPTIONS. Bordoloi and Bock Errors Two types of errors can be found in a program: compilation errors and runtime errors. There is.
Cursor and Exception Handling By Nidhi Bhatnagar.
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:
Exceptions Oracle Database PL/SQL 10g Programming Chapter 7.
EE Copyright س Oracle Corporation, All rights reserved. ® Review of PL/SQL.
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.
ISYS 365 – SQL*Plus Environment. 2 Agenda What is SQL*Plus? Command Line Editor Useful SQL*Plus Commands Useful System Tables What is PL/SQL? PL/SQL Constructs.
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.
CS178 Database Management PL/SQL session 8 References: ORACLE 9i PROGRAMMING A Primer Rajshekhar Sunderraman.
CSIT 313 DB PROGRAMMING EXCEPTION HANDLING. In PL/SQL, an error condition is called an exception. An exception can be either –internally defined (by the.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Oracle PL/SQL. PL/SQL Originally modeled after ADA Originally modeled after ADA Created for Dept. of DefenseCreated for Dept. of Defense Allows expanded.
CIS4368: Advanced DatabaseSlide # 1 PL/SQL Dr. Peeter KirsSpring, 2003 PL/SQL.
Trapping Oracle Server Exceptions. 2 home back first prev next last What Will I Learn? Describe and provide an example of an error defined by the Oracle.
PL/SQL Block Structure DECLARE - Optional Variables, cursors, user-defined exceptions BEGIN - Mandatory SQL Statements PL/SQL Statements EXCEPTIONS - Optional.
Chapter 15 Introduction to PL/SQL. Chapter Objectives  Explain the benefits of using PL/SQL blocks versus several SQL statements  Identify the sections.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 3 Handling Data in PL/SQL Blocks.
Handling Exceptions. 2 home back first prev next last What Will I Learn? Describe several advantages of including exception handling code in PL/SQL Describe.
Oracle 8i Exception Handling. General Syntax DECLARE --- BEGIN --- EXCEPTION WHEN exception_name1 THEN -Error handling statements WHEN exception_name2.
Objectives Database triggers and syntax
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 9 Database Triggers.
Using SQL in PL/SQL ITEC 224 Database Programming.
© 2007 by Prentice Hall7-1 Introduction to Oracle 10g Chapter 7 Using PL/SQL to Your Advantage James Perry and Gerald Post.
PL/SQLPL/SQL Oracle11g: PL/SQL Programming Chapter 4 Cursors and Exception Handling.
1 Handling Exceptions Part F. 2 Handling Exceptions with PL/SQL What is an exception? Identifier in PL/SQL that is raised during execution What is an.
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.
Chapter 18: Exception Handling1 Chapter Eighteen Exception Handling Objective: – Define exceptions – List types of exception handlers – Trap errors – Exception.
Handling Exceptions. Objectives What is exception Types of exceptions How to handle exceptions Trapping pre defined oracle errors.
Copyright  Oracle Corporation, All rights reserved. 23 Handling Exceptions.
Oracle9i Developer: PL/SQL Programming Chapter 5 Functions.
Oracle 数据库应用 -- PL/SQL 进阶 (3) & Oracle DBA 2016/5/ /5/10.
Oracle9i Developer: PL/SQL Programming Chapter 6 PL/SQL Packages.
1 Copyright © 2004, Oracle. All rights reserved. PL/SQL Programming Concepts: Review.
Free Powerpoint Templates Page 1 Free Powerpoint Templatesبسم الله الرحمن الرحيم عدد الساعات: 2 نظري+2عملي الرمز:314 حسب المتطلبات:223 حسب (مبادئ قواعد.
ITEC 224 Database Programming
CHAPTER 5 EXCEPTION HANDLING
Fundamentals of PL/SQL part 2 (Basics)
Handling Exceptions.
Oracle11g: PL/SQL Programming Chapter 2 Basic PL/SQL Block Structures.
Oracle11g: PL/SQL Programming Chapter 4 Cursors and Exception Handling.
Handling Exceptions.
Database Programming PL SQL.
Agenda Summary of last class Cursors Loops Records Simple Loops
Handling Exceptions.
Handling Exceptions.
Chapter 2 Handling Data in PL/SQL Blocks Oracle9i Developer:
Database Management Systems 2
Handling Exceptions.
Chapter 1 Introduction to PL/SQL Oracle10g Developer:
Oracle9i Developer: PL/SQL Programming Chapter 8 Database Triggers.
Prof. Arfaoui. COM390 Chapter 9
Prof. Arfaoui. COM390 Chapter 6
Handling Data in PL/SQL Blocks
Prof. Arfaoui. COM390 Chapter 7
Database Programming Using Oracle 11g
Presentation transcript:

Oracle9i Developer: PL/SQL Programming Chapter 3 PL/SQL Processing

Chapter Objectives Use control structures to make decisions Use looping structures to repeat code Work with the GOTO statement Manage errors with exception handlers Address exception-handling issues, such as RAISE_APPLICATION_ERROR and propagation Document code with comments

Brewbean’s Challenge

Control Structures IF Statements CASE Statements Basic IF IF/THEN/ELSE IF/THEN/ELSIF/ELSE CASE Statements Basic CASE statement Searched CASE statement CASE expression

Simple IF Statement

Add the ELSE clause

Add ELSIF clauses

NOT ELSEIF

Can check multiple conditions IF rec_order.state IN ('VA','PA','ME') THEN lv_tax_num := rec_order.sub * .06; ELSE lv_tax_num := rec_order.sub * .04; END IF;

Basic CASE Statement

No CASE Found Error

Searched CASE Statement

CASE Expression

Basic Loop

WHILE Loop

FOR Loop

Use Variables in FOR Loop DECLARE lv_upper_num NUMBER(3); BEGIN SELECT COUNT(idBasket) INTO lv_upper_num FROM bb_basket; FOR i IN 1..lv_upper_num LOOP DBMS_OUTPUT.PUT_LINE(i); END LOOP; END;

Reverse Option in FOR Loop Instructs counter to start with the upper bound value and increment by –1 to the lower bound value Example: FOR I IN REVERSE 1..5 LOOP

GOTO Statement Jumping control that instructs the program to move to another area of code to continue processing Most developers discourage the use of GOTO as it complicates the flow of execution

Exception Handlers Used to capture error conditions and handle the processing to allow the application to continue Placed in the EXCEPTION section of a PL/SQL block Two types of errors: Oracle errors (Predefined and Non-Predefined) User-defined errors RAISE_APPLICATION_ERROR

Predefined Oracle Errors Exception Name Description NO_DATA_FOUND A SELECT statement in a PL/SQL block retrieves no rows or a nonexistent row of an index-by table is referenced. TOO_MANY_ROWS A SELECT statement in a PL/SQL block retrieves more than one row. CASE_NOT_FOUND No WHEN clause in the CASE statement is processed. ZERO_DIVIDE Attempted division by zero. DUP_VAL_ON_INDEX Attempted violation of a unique or primary key column constraint.

Predefined Error Example

Add Exception Handler

Non-Predefined Handler Declare an exception Associate an Oracle error number to the exception name Foreign key error occurs if item lines are still in the bb_basketitem table Exception handler executes if Foreign key error –2292 is raised by the DELETE

User-Defined Exception Declare an exception If no rows updated, raise the exception Exception handler

Additional Exception Concepts WHEN OTHERS – traps all errors not specifically addressed by an exception handler and used for handling unanticipated errors SQLCODE and SQLERRM – Functions used to identify the error code and message especially in application testing to identify unanticipated errors

SQLCODE and SQLERRM Log error information to a database table using these functions

RAISE_APPLICATION_ERROR Allows developers to associate their own error number and message to an error Can only be used with stored programs which is covered in Chapter 4

Exception Propagation When an error occurs in a block, processing immediately moves to the EXCEPTION section of the block If the block is nested, and no handlers address the error, the processing will then move to the EXCEPTION section of the enclosing block

Commenting Code Add comments within code to identify code purpose and processing steps Use /* */ to enclose a multiline comment Use -- to add a single or partial line comment

Comment Examples DECLARE ex_prod_update EXCEPTION; --For UPDATE of no rows exception BEGIN /* This block is used to update product descriptions Constructed to support the Prod_desc.frm app screen Exception raised if no rows updated */ UPDATE bb_product SET description = 'Mill grinder with 5 grind settings!' WHERE idProduct = 30; --Check if any rows updated IF SQL%NOTFOUND THEN RAISE ex_prod_update; END IF; EXCEPTION WHEN ex_prod_update THEN DBMS_OUTPUT.PUT_LINE('Invalid product id entered'); END;

Summary Add conditional logic using IF and CASE statements Repeat lines of code using Loop structures Add error handlers in the EXCEPTION area to manage Oracle and user-defined errors Use comments in code for documentation