Database Programming with PL/SQL

Similar documents
PL/SQL. Exception. When the PL/SQL engine cannot execute the PLSQL block it raise an error. Every Oracle error has an error number

Sisteme Informatice şi Standarde Deschise (SISD) Curs 7 Standarde pentru programarea bazelor de date (1)

Database Programming with PL/SQL

ORACLE: PL/SQL Programming

Lab Assignment 9 CIS 208A PL/SQL Programming and SQL

PLSQL Interview Questions :

Lesson B Objectives IF/THEN. Chapter 4B: More Advanced PL/SQL Programming

UNIT II PL / SQL AND TRIGGERS

Database Programming with PL/SQL

Oracle Class VI. Exception Block Cursors For Loops

PL/SQL Block structure

to use this Student Guide

Question No : 1 Which statement is true about triggers on data definition language (DDL) statements?

Rezolvarea exceptiilor

Database Modelling. Lecture 8 (b): PL/SQL Exceptions Handling 1/6/2015 1

Oracle PL/SQL Best Practices Part 1. John Mullins

Database Programming with PL/SQL

Database Programming with PL/SQL

ITDUMPS QUESTION & ANSWER. Accurate study guides, High passing rate! IT dumps provides update free of charge in one year!

Manipulating Data. Copyright 2004, Oracle. All rights reserved.

PLSQL 9i Index. Section Title Page

Actual4Test. Actual4test - actual test exam dumps-pass for IT exams

Denys Xavier denys org br) has a non-transferable license to use this Student Guide

Weird PL/SQL Steven Feuerstein PL/SQL Evangelist, Quest Software

Retrieving Data Using the SQL SELECT Statement. Copyright 2004, Oracle. All rights reserved.

Section I : Section II : Question 1. Question 2. Question 3.

Database Foundations. 6-4 Data Manipulation Language (DML) Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Data Manipulation Language

Introduction to Explicit Cursors. Copyright 2008, Oracle. All rights reserved.

Oracle 1z z0-146 Oracle Database 11g: Advanced PL/SQL. Practice Test. Version QQ:

Táblák tartalmának módosítása. Copyright 2004, Oracle. All rights reserved.

Course Overview. Copyright 2010, Oracle and/or its affiliates. All rights reserved.

Course 492 Supplementary Materials. Mutating Tables

IT Certification Exams Provider! Weofferfreeupdateserviceforoneyear! h ps://

Banner Oracle PL/SQL and Database Objects Training Workbook

RETRIEVING DATA USING THE SQL SELECT STATEMENT

Meet MariaDB Vicențiu Ciorbaru Software MariaDB Foundation * * 2017 MariaDB Foundation

REF Cursors. Oracle University and Egabi Solutions use only

Database Programming with PL/SQL

PROCEDURAL DATABASE PROGRAMMING ( PL/SQL AND T-SQL)

Programming in Oracle with PL/SQL. Procedural Language Extension to SQL

Retrieving Data Using the SQL SELECT Statement. Copyright 2004, Oracle. All rights reserved.

Oracle PLSQL. Course Summary. Duration. Objectives

Oracle Class VII More on Exception Sequence Triggers RowID & Rownum Views

Database Programming with SQL

MIS 409 PL/SQL test (May 5, 2016) 1) Create a package called Company_package - This package consists of two procedures and two functions.

Database Programming with SQL

Table of Contents. Oracle SQL PL/SQL Training Courses

Database Programming with PL/SQL

1Z0-144 Q&As Oracle Database 11g: Program with PL/ SQL

Oracle. PL/SQL Procedural Language

Database Programming with PL/SQL

2. Programming written ( main theme is to test our data structure knowledge, proficiency

ORACLE Job Placement Paper. Paper Type : General - other

Database Programming with PL/SQL

RNDr. Michal Kopecký, Ph.D. Department of Software Engineering, Faculty of Mathematics and Physics, Charles University in Prague

Database Programming with SQL

Introduction to Computer Science and Business

Contents I Introduction 1 Introduction to PL/SQL iii

Database Programming with PL/SQL

Business Analytics. SQL PL SQL [Oracle 10 g] P r i n c e S e t h i w w w. x l m a c r o. w e b s. c o m

PL/SQL is a combination of SQL along with the procedural features of programming languages.

Database Programming with PL/SQL

Question Bank PL/SQL Fundamentals-I

Lab 09: Advanced SQL

PL / SQL Basics. Chapter 3

Oracle Database 11g & PL/SQL

Table of Contents. PDF created with FinePrint pdffactory Pro trial version

Using the Set Operators. Copyright 2006, Oracle. All rights reserved.

DEFAULT Values, MERGE, and Multi-Table Inserts. Copyright 2009, Oracle. All rights reserved.

Retrieving Data Using the SQL SELECT Statement. Copyright 2009, Oracle. All rights reserved.

Slides by: Ms. Shree Jaswal

Greens Technology s Contact :

Full file at

1Z0-007 ineroduction to oracle9l:sql

Oracle Database 11g: PL/SQL Fundamentals

BraindumpsVCE. Best vce braindumps-exam vce pdf free download

Oracle Exam 1z0-144 Oracle Database 11g: Program with PL/SQL Version: 8.5 [ Total Questions: 103 ]

1Z Oracle Database 11g - Program with PL/SQL Exam Summary Syllabus Questions

Oracle Database 11g: SQL and PL/SQL Fundamentals

First lecture of this chapter is in slides (PPT file)

5. Single-row function

Introduction to Oracle9i: SQL

Performance and Tuning. 2010, Oracle and/or its affiliates. All rights reserved.

Database Programming - Section 18. Instructor Guide

Oracle PLSQL Training Syllabus

Appendix A Practices and Solutions

Oracle PL/SQL - 12c & 11g [Basic PL/SQL & Advanced PL/SQL]

Oracle EXAM 1Z0-144 Oracle Database 11g: Program with PL/SQL

Restricting and Sorting Data. Copyright 2004, Oracle. All rights reserved.

Oracle 1Z Oracle Fusion Middleware 11g- Build Applications with Oracle Forms.

1Z0-144.v Number: 1Z0-144 Passing Score: 800 Time Limit: 120 min File Version:

KATHERYNE MERIVETH AMARO MEDRANO. this Student Guide

COMP 430 Intro. to Database Systems. Encapsulating SQL code

Oracle Database: SQL and PL/SQL Fundamentals NEW

Bsc (Hons) Software Engineering. Examinations for / Semester 1. Resit Examinations for BSE/15A/FT & BSE/16A/FT

based mainly on course notes by Will Hyslop (UNB-F) and course notes by Jeff Ullman and others (Stanford)

Oracle Database: SQL and PL/SQL Fundamentals Ed 2

Exam Name: Oracle Database 11g: Program with PL/SQL

7. PL/SQL. 7.1 Introduction. PL/SQL = Procedural Language extensions to SQL

Transcription:

Database Programming with PL/SQL Trapping Oracle Server Exceptions 1 Copyright 2013, Oracle and/or its affiliates. All rights

Objectives This lesson covers the following objectives: Describe and provide an example of an error defined by the Oracle server Describe and provide an example of an error defined by the PL/SQL programmer Differentiate between errors that are handled implicitly and explicitly by the Oracle server Write PL/SQL code to trap a predefined Oracle server error 2 Copyright 2013, Oracle and/or its affiliates. All rights

Objectives (cont.) This lesson covers the following objectives: Write PL/SQL code to trap a non-predefined Oracle server error Write PL/SQL code to identify an exception by error code and by error message 3 Copyright 2013, Oracle and/or its affiliates. All rights

Purpose PL/SQL error handling is flexible and allows programmers to use both errors defined by the Oracle server and errors defined by the programmer. This lesson discusses predefined and non-predefined Oracle server errors. Predefined errors are the common Oracle errors for which PL/SQL has predefined exception names. Nonpredefined errors make use of the ORA error codes and messages. The syntax is different for each, but you can trap both kinds of errors in the EXCEPTION section of your PL/SQL program. 4 Copyright 2013, Oracle and/or its affiliates. All rights

Exception Types This lesson discusses predefined and non-predefined Oracle server exceptions. Exception Predefined Oracle server error Non-predefined Oracle server error User-defined error Description One of approximately 20 errors that occur most often in PL/SQL code Any other standard Oracle server error A condition that the PL/SQL programmer decides is abnormal Instructions for Handling You need not declare these exceptions. They are predefined by the Oracle server and are raised implicitly (automatically). Declare within the declarative section and allow the Oracle Server to raise them implicitly (automatically). Declare within the declarative section, and raise explicitly. 5 Copyright 2013, Oracle and/or its affiliates. All rights

Handling Exceptions with PL/SQL There are two methods for raising an exception: Implicitly (automatically) by the Oracle server: An Oracle error occurs and the associated exception is raised automatically. For example, if the error ORA-01403 occurs when no rows are retrieved from the database in a SELECT statement, then PL/SQL raises the exception NO_DATA_FOUND. 6 Copyright 2013, Oracle and/or its affiliates. All rights

Handling Exceptions with PL/SQL (cont.) Explicitly by the programmer: Depending on the business functionality your program is implementing, you might have to explicitly raise an exception. You raise an exception explicitly by issuing the RAISE statement within the block. The exception being raised can be either user-defined or predefined. These are explained in the next lesson. 7 Copyright 2013, Oracle and/or its affiliates. All rights

Two Types of Oracle Server Errors When an Oracle server error occurs, the Oracle server automatically raises the associated exception, skips the rest of the executable section of the block, and looks for a handler in the exception section. 8 Copyright 2013, Oracle and/or its affiliates. All rights

Two Types of Oracle Server Errors (cont.) There are two types of Oracle server errors: Predefined Oracle server errors: Each of these errors has a predefined name. For example, if the error ORA-01403 occurs when no rows are retrieved from the database in a SELECT statement, then PL/SQL raises the predefined exception-name NO_DATA_FOUND. 9 Copyright 2013, Oracle and/or its affiliates. All rights

Two Types of Oracle Server Errors (cont.) Non-predefined Oracle server errors: Each of these errors has a standard Oracle error number (ORA-nnnnn) and error message, but not a predefined name. You declare your own names for these so that you can reference these names in the exception section. 10 Copyright 2013, Oracle and/or its affiliates. All rights

Trapping Predefined Oracle Server Errors Reference the predefined name in the exception handling routine. Sample predefined exceptions: NO_DATA_FOUND TOO_MANY_ROWS INVALID_CURSOR ZERO_DIVIDE DUP_VAL_ON_INDEX 11 Copyright 2013, Oracle and/or its affiliates. All rights

Trapping Predefined Oracle Server Errors (cont.) For a partial list of predefined exceptions, refer to the short list available from the Student Resources in Section 0. For a complete list of predefined exceptions, see the PL/SQL User s Guide and Reference. 12 Copyright 2013, Oracle and/or its affiliates. All rights

Trapping Predefined Oracle Server Errors (cont.) The following example uses the TOO_MANY_ROWS predefined Oracle server error. Note that it is not declared in the DECLARATION section. DECLARE v_lname VARCHAR2(15); BEGIN SELECT last_name INTO v_lname FROM employees WHERE job_id = 'ST_CLERK'; DBMS_OUTPUT.PUT_LINE('The last name of the ST_CLERK is: ' v_lname); EXCEPTION WHEN TOO_MANY_ROWS THEN DBMS_OUTPUT.PUT_LINE (' Your select statement retrieved multiple rows. Consider using a cursor.'); END; 13 Copyright 2013, Oracle and/or its affiliates. All rights

Trapping Several Predefined Oracle Server Errors This example handles TOO_MANY_ROWS and NO_DATA_FOUND, with an OTHERS handler in case any other error occurs. DECLARE v_lname VARCHAR2(15); BEGIN SELECT last_name INTO v_lname FROM employees WHERE job_id = 'ST_CLERK'; DBMS_OUTPUT.PUT_LINE('The last name of the ST_CLERK is: ' v_lname); EXCEPTION WHEN TOO_MANY_ROWS THEN DBMS_OUTPUT.PUT_LINE ('Select statement found multiple rows'); WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE ('Select statement found no rows'); WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE ('Another type of error occurred'); END; 14 Copyright 2013, Oracle and/or its affiliates. All rights

Trapping Non-Predefined Oracle Server Errors Non-predefined exceptions are similar to predefined exceptions; however, they do not have predefined names in PL/SQL. They are standard Oracle server errors and have ORA- error numbers. You create your own names for them in the DECLARE section and associate these names with ORA- error numbers using the PRAGMA EXCEPTION_INIT function. 15 Copyright 2013, Oracle and/or its affiliates. All rights

Why is Exception Handling Important? (cont.) Declare Associate Reference Declarative section EXCEPTION section Name the exception Code PRAGMA EXCEPTION_INIT Handle the raised exception 16 Copyright 2013, Oracle and/or its affiliates. All rights

Trapping Non-Predefined Oracle Server Errors (cont.) You can trap a non-predefined Oracle server error by declaring it first. The declared exception is raised implicitly. In PL/SQL, the PRAGMA EXCEPTION_INIT tells the compiler to associate an exception name with an Oracle error number. This allows you to refer to any Oracle Server exception by name and to write a specific handler for it. 17 Copyright 2013, Oracle and/or its affiliates. All rights

Non-Predefined Error Examine the following example. BEGIN INSERT INTO departments (department_id, department_name) VALUES (280, NULL); END; ORA-01400: cannot insert NULL into ( USVA_TEST_SQL01_S01. DEPARTMENTS. DEPARTMENT_NAME ) 18 Copyright 2013, Oracle and/or its affiliates. All rights

Non-Predefined Error (cont.) The INSERT statement tries to insert the value NULL for the department_name column of the departments table. However, the operation is not successful because department_name is a NOT NULL column. There is no predefined error name for violating a NOT NULL constraint. The way to work around this problem is to declare you own name and associate it with the ORA-01400 error. 19 Copyright 2013, Oracle and/or its affiliates. All rights

Non-Predefined Error (cont.) 1. Declare the name of the exception in the declarative section. DECLARE 1 e_insert_excep EXCEPTION; PRAGMA EXCEPTION_INIT 2 (e_insert_excep, -01400); BEGIN INSERT INTO departments (department_id, department_name) VALUES (280, NULL); EXCEPTION WHEN e_insert_excep 3 THEN DBMS_OUTPUT.PUT_LINE('INSERT FAILED'); END; Syntax: exception name EXCEPTION; where EXCEPTION is the name of the exception 20 Copyright 2013, Oracle and/or its affiliates. All rights

Non-Predefined Error (cont.) 2. Associate the declared exception with the standard Oracle server error number using the PRAGMA EXCEPTION_INIT function. DECLARE 1 e_insert_excep EXCEPTION; PRAGMA EXCEPTION_INIT 2 (e_insert_excep, -01400); BEGIN INSERT INTO departments (department_id, department_name) VALUES (280, NULL); EXCEPTION WHEN e_insert_excep 3 THEN DBMS_OUTPUT.PUT_LINE('INSERT FAILED'); END; PRAGMA EXCEPTION_INIT (exception, error_number); where exception is the previously declared exception name and error_number is a standard Oracle server error number, including the hyphen in front of it. 21 Copyright 2013, Oracle and/or its affiliates. All rights

Non-Predefined Error (cont.) 3. Reference the declared exception name within the corresponding exception-handling routine. DECLARE 1 e_insert_excep EXCEPTION; PRAGMA EXCEPTION_INIT 2 (e_insert_excep, -01400); BEGIN INSERT INTO departments (department_id, department_name) VALUES (280, NULL); EXCEPTION WHEN e_insert_excep 3 THEN DBMS_OUTPUT.PUT_LINE('INSERT FAILED'); END; 22 Copyright 2013, Oracle and/or its affiliates. All rights

Functions for Trapping Exceptions When an exception occurs, you can retrieve the associated error code or error message by using two functions. Based on the values of the code or the message, you can decide which subsequent actions to take. SQLERRM returns character data containing the message associated with the error number. SQLCODE returns the numeric value for the error code. (You can assign it to a NUMBER variable.) 23 Copyright 2013, Oracle and/or its affiliates. All rights

Functions for Trapping Exceptions (cont.) SQLCODE Value Description 0 No exception encountered 1 User defined exception +100 NO_DATA_FOUND exception Negative number Another Oracle Server error number 24 Copyright 2013, Oracle and/or its affiliates. All rights

Functions for Trapping Exceptions (cont.) You cannot use SQLCODE or SQLERRM directly in an SQL statement. Instead, you must assign their values to local variables, then use the variables in the SQL statement, as shown in the following example: DECLARE v_error_code NUMBER; v_error_message VARCHAR2(255); BEGIN... EXCEPTION WHEN OTHERS THEN ROLLBACK; v_error_code := SQLCODE ; v_error_message := SQLERRM ; END; INSERT INTO error_log(e_user,e_date,error_code,error_message) VALUES(USER,SYSDATE,v_error_code,v_error_message); 25 Copyright 2013, Oracle and/or its affiliates. All rights

Terminology Key terms used in this lesson included: Non-predefined Oracle server errors Predefined Oracle server errors PRAGMA EXCEPTION_INIT SQLERRM SQLCODE 26 Copyright 2013, Oracle and/or its affiliates. All rights

Summary In this lesson, you should have learned how to: Describe and provide an example of an error defined by the Oracle server. Describe and provide an example of an error defined by the PL/SQL programmer Differentiate between errors that are handled implicitly and explicitly by the Oracle server Write PL/SQL code to trap a predefined Oracle server error 27 Copyright 2013, Oracle and/or its affiliates. All rights

Summary (cont.) In this lesson, you should have learned how to: Write PL/SQL code to trap a non-predefined Oracle server error Write PL/SQL code to identify an exception by error code and by error message 28 Copyright 2013, Oracle and/or its affiliates. All rights