1 Copyright © 2004, Oracle. All rights reserved. PL/SQL Programming Concepts: Review.

Slides:



Advertisements
Similar presentations
BD05/06 PL/SQL  Introduction  Structure of a block  Variables and types  Accessing the database  Control flow  Cursors  Exceptions  Procedures.
Advertisements

Chapter 4B: More Advanced PL/SQL Programming
3 Copyright © 2004, Oracle. All rights reserved. Creating Packages.
1 Copyright © 2004, Oracle. All rights reserved. Creating Stored Procedures.
Creating Packages. 2 home back first prev next last What Will I Learn? Describe the reasons for using a package Describe the two components of a package:
A Guide to Oracle9i1 Advanced SQL And PL/SQL Topics Chapter 9.
Programming in Oracle with PL/SQL
2 Copyright © 2004, Oracle. All rights reserved. Creating Stored Functions.
Copyright  Oracle Corporation, All rights reserved. 4 Creating Functions.
Advanced Databases Advanced PL/SQL Programming: Procedure, Function and Package.
Bordoloi and Bock CURSORS. Bordoloi and Bock CURSOR MANIPULATION To process an SQL statement, ORACLE needs to create an area of memory known as the context.
PL / SQL P rocedural L anguage / S tructured Q uery L anguage Chapter 7 in Lab Reference.
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.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 7 PL/SQL Packages.
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.
Overview · What is PL/SQL · Advantages of PL/SQL · Basic Structure of a PL/SQL Block · Procedure · Function · Anonymous Block · Types of Block · Declaring.
Lecture 8 Creating Stored Functions. Objectives  After completing this lesson, you should be able to do the following:  What is Function?  Types of.
L/O/G/O Working with Composite Data Types. Objectives After completing this lesson, you should be able to do the following: –Create user-defined PL/SQL.
PRACTICE OVERVIEW PL/SQL Part Examine this package specification and body: Which statement about the V_TOTAL_BUDGET variable is true? A. It must.
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.
Oracle PL/SQL. PL/SQL Originally modeled after ADA Originally modeled after ADA Created for Dept. of DefenseCreated for Dept. of Defense Allows expanded.
1 Copyright © 2004, Oracle. All rights reserved. Introduction to 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.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
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.
Database Application Development using PL/SQL Programming.
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.
Copyright  Oracle Corporation, All rights reserved. 16 Declaring Variables.
Oracle 8i Exception Handling. General Syntax DECLARE --- BEGIN --- EXCEPTION WHEN exception_name1 THEN -Error handling statements WHEN exception_name2.
What is a Package? A package is an Oracle object, which holds other objects within it. Objects commonly held within a package are procedures, functions,
Using Oracle-Supplied Packages. 2 home back first prev next last What Will I Learn? Describe two common uses for the DBMS_OUTPUT server-supplied package.
ITEC 224 Database Programming PL/SQL Lab Cursors.
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 
Introduction to Explicit Cursors. 2 home back first prev next last What Will I Learn? Distinguish between an implicit and an explicit cursor Describe.
implicit and an explicit cursor
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.
PRACTICE OVERVIEW PL/SQL Part Your stored procedure, GET_BUDGET, has a logic problem and must be modified. The script that contains the procedure.
Oracle10g Developer: PL/SQL Programming1 Objectives Named program units How to identify parameters The CREATE PROCEDURE statement Creating a procedure.
Creating Procedures. PL/SQL Program Construct Tools Constructs Anonymous Block Application procedures or functions Application packages Application Triggers.
Copyright  Oracle Corporation, All rights reserved. 23 Handling Exceptions.
Text TCS INTERNAL Oracle PL/SQL – Introduction. TCS INTERNAL PL SQL Introduction PLSQL means Procedural Language extension of SQL. PLSQL is a database.
Program with PL/SQL Lesson 3. Interacting with the Oracle Server.
Kingdom of Saudi Arabia Ministry of Higher Education Al-Imam Muhammad Ibn Saud Islamic University College of Computer and Information Sciences Overview.
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.
Stored Procedures and Functions Pemrograman Basis Data MI2183.
STORED PROCEDURE & STORED FUNCTION Politeknik Telkom 2012.
1 Chapter 5: Advanced PL/SQL Programming. 2 Anonymous PL/SQL Programs Write code in text editor, execute it in SQL*Plus Code can be stored as text in.
Oracle9i Developer: PL/SQL Programming Chapter 6 PL/SQL Packages.
Preface IIntroduction Course Objectives I-2 Oracle Complete Solution I-3 Course Agenda I-4 Tables Used in This Course I-5 The Order Entry Schema I-6 The.
Creating Stored Functions
Oracle11g: PL/SQL Programming Chapter 5 Procedures.
PL/SQL.
UNIT - V STORED PROCEDURE.
Handling Exceptions.
PL/SQL Scripting in Oracle:
PL/SQL Programming Concepts: Review
Handling Exceptions.
Handling Exceptions.
Database Management Systems 2
Handling Exceptions.
Chapter 8 Advanced SQL.
Prof. Arfaoui. COM390 Chapter 7
Presentation transcript:

1 Copyright © 2004, Oracle. All rights reserved. PL/SQL Programming Concepts: Review

1-2 Copyright © 2004, Oracle. All rights reserved. Objectives After completing this lesson, you should be able to do the following: Identify PL/SQL block structure Describe PL/SQL basics List restrictions and guidelines on calling functions from SQL expressions Identify how explicit cursors are processed Handle exceptions Use the raise_application_error procedure Manage dependencies

1-3 Copyright © 2004, Oracle. All rights reserved. DECLARE BEGIN EXCEPTION END; PL/SQL Block Structure IS|AS DECLARE BEGIN EXCEPTION END; Anonymous PL/SQL block Stored program unit

1-4 Copyright © 2004, Oracle. All rights reserved. Naming Conventions Advantages of proper naming conventions: Easier to read Understandable Give information about the functionality Easier to debug Ensure consistency

1-5 Copyright © 2004, Oracle. All rights reserved. Procedures A procedure is: A named PL/SQL block that performs a sequence of actions Stored in the database as a schema object Used to promote reusability and maintainability CREATE [OR REPLACE] PROCEDURE procedure_name [(parameter1 [mode] datatype1, parameter2 [mode] datatype2,...)] IS|AS [local_variable_declarations; …] BEGIN -- actions; END [procedure_name];

1-6 Copyright © 2004, Oracle. All rights reserved. Functions A function is: A block that returns a value Stored in the database as a schema object Called as part of an expression or used to provide a parameter value CREATE [OR REPLACE] FUNCTION function_name [(parameter1 [mode1] datatype1,...)] RETURN datatype IS|AS [local_variable_declarations; …] BEGIN -- actions; RETURN expression; END [function_name];

1-7 Copyright © 2004, Oracle. All rights reserved. Function: Example Create the function: Invoke the function as an expression or as a parameter value: CREATE OR REPLACE FUNCTION get_credit (v_id customers.customer_id%TYPE) RETURN NUMBER IS v_credit customers.credit_limit%TYPE := 0; BEGIN SELECT credit_limit INTO v_credit FROM customers WHERE customer_id = v_id; RETURN v_credit; END get_credit; / EXECUTE dbms_output.put_line(get_credit(101))

1-8 Copyright © 2004, Oracle. All rights reserved. Ways to Execute Functions Invoke as part of a PL/SQL expression –Using a host variable to obtain the result: –Using a local variable to obtain the result: Use as a parameter to another subprogram Use in a SQL statement (subject to restrictions) EXECUTE dbms_output.put_line(get_credit(101)) SELECT get_credit(customer_id) FROM customers; VARIABLE v_credit NUMBER EXECUTE :v_credit := get_credit(101) DECLARE v_credit customers.credit_limit%type; BEGIN v_credit := get_credit(101);... END;

1-9 Copyright © 2004, Oracle. All rights reserved. Restrictions on Calling Functions from SQL Expressions User-defined functions that are callable from SQL expressions must: –Be stored in the database –Accept only IN parameters with valid SQL data types, not PL/SQL-specific types –Return valid SQL data types, not PL/SQL-specific types When calling functions in SQL statements: –Parameters must be specified with positional notation –You must own the function or have the EXECUTE privilege

1-10 Copyright © 2004, Oracle. All rights reserved. Guidelines for Calling Functions from SQL Expressions Functions called from: A SELECT statement cannot contain DML statements An UPDATE or DELETE statement on a table T cannot query or contain DML on the same table T SQL statements cannot end transactions (that is, cannot execute COMMIT or ROLLBACK operations) Note: Calls to subprograms that break these restrictions are also not allowed in the function.

1-11 Copyright © 2004, Oracle. All rights reserved. PL/SQL Packages: Review PL/SQL packages: Group logically related components: –PL/SQL types –Variables, data structures, and exceptions –Subprograms: procedures and functions Consist of two parts: –A specification –A body Enable the Oracle server to read multiple objects into memory simultaneously

1-12 Copyright © 2004, Oracle. All rights reserved. Components of a PL/SQL Package Package specification Package body Procedure A declaration; variable Procedure A definition BEGIN … END; Procedure B definition … variable Public Private

1-13 Copyright © 2004, Oracle. All rights reserved. Syntax: The OR REPLACE option drops and re-creates the package specification. Variables declared in the package specification are initialized to NULL by default. All the constructs declared in a package specification are visible to users who are granted privileges on the package. CREATE [OR REPLACE] PACKAGE package_name IS|AS public type and variable declarations subprogram specifications END [package_name]; Creating the Package Specification

1-14 Copyright © 2004, Oracle. All rights reserved. Creating the Package Body Syntax: The OR REPLACE option drops and re-creates the package body. Identifiers defined in the package body are private and not visible outside the package body. All private constructs must be declared before they are referenced. Public constructs are visible to the package body. CREATE [OR REPLACE] PACKAGE BODY package_name IS|AS private type and variable declarations subprogram bodies [BEGIN initialization statements] END [package_name];

1-15 Copyright © 2004, Oracle. All rights reserved. Cursor A cursor is a pointer to the private memory area allocated by the Oracle server. There are two types of cursors: –Implicit cursors: Created and managed internally by the Oracle server to process SQL statements –Explicit cursors: Explicitly declared by the programmer

1-16 Copyright © 2004, Oracle. All rights reserved.

1-17 Copyright © 2004, Oracle. All rights reserved. Processing Explicit Cursors The following three commands are used to process an explicit cursor: OPEN FETCH CLOSE Alternatively, you can also use a cursor FOR loops.

1-18 Copyright © 2004, Oracle. All rights reserved. Explicit Cursor Attributes Every explicit cursor has the following four attributes: cursor_name%FOUND cursor_name%ISOPEN cursor_name%NOTFOUND cursor_name%ROWCOUNT

1-19 Copyright © 2004, Oracle. All rights reserved. Cursor FOR Loops Syntax: The cursor FOR loop is a shortcut to process explicit cursors. Implicit open, fetch, exit, and close occur. The record is implicitly declared. FOR record_name IN cursor_name LOOP statement1; statement2;... END LOOP;

1-20 Copyright © 2004, Oracle. All rights reserved. Cursor: Example SET SERVEROUTPUT ON DECLARE CURSOR cur_cust IS SELECT cust_first_name, credit_limit FROM customers WHERE credit_limit > 4000; BEGIN LOOP DBMS_OUTPUT.PUT_LINE ( ||' '|| ); END LOOP; END; / FOR v_cust_record IN cur_cust v_cust_record.cust_first_name v_cust_record.credit_limit

1-21 Copyright © 2004, Oracle. All rights reserved. Handling Exceptions An exception is an error in PL/SQL that is raised during program execution. An exception can be raised: –Implicitly by the Oracle server –Explicitly by the program An exception can be handled: –By trapping it with a handler –By propagating it to the calling environment

1-22 Copyright © 2004, Oracle. All rights reserved. Handling Exceptions Exception raised Is the exception trapped? yes Execute statements in the EXCEPTION section. Terminate gracefully. no Terminate abruptly. Propagate the exception.

1-23 Copyright © 2004, Oracle. All rights reserved. Exceptions: Example DECLARE v_lname VARCHAR2(15); BEGIN SELECT cust_last_name INTO v_lname FROM customers WHERE cust_first_name='Ally'; DBMS_OUTPUT.PUT_LINE ('Ally''s last name is : ' ||v_lname); END; / EXCEPTION WHEN TOO_MANY_ROWS THEN DBMS_OUTPUT.PUT_LINE (' Your select statement retrieved multiple rows. Consider using a cursor.');

1-24 Copyright © 2004, Oracle. All rights reserved. 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

1-25 Copyright © 2004, Oracle. All rights reserved.

1-26 Copyright © 2004, Oracle. All rights reserved.

1-27 Copyright © 2004, Oracle. All rights reserved. Trapping Non-Predefined Oracle Server Errors Declarative section Declare Name the exception Code PRAGMA EXCEPTION_INIT EXCEPTION section Handle the raised exception Associate Reference

1-28 Copyright © 2004, Oracle. All rights reserved. Trapping User-Defined Exceptions Declarative section Executable section Exception-handling section Declare Raise Reference Name the exception. Explicitly raise the exception by using the RAISE statement. Handle the raised exception.

1-29 Copyright © 2004, Oracle. All rights reserved. The RAISE_APPLICATION_ERROR Procedure Syntax: You can use this procedure to issue user-defined error messages from stored subprograms. You can report errors to your application and avoid returning unhandled exceptions. raise_application_error (error_number, message[, {TRUE | FALSE}]);

1-30 Copyright © 2004, Oracle. All rights reserved. The RAISE_APPLICATION_ERROR Procedure Is used in two different places: –Executable section –Exception section Returns error conditions to the user in a manner consistent with other Oracle server errors.

1-31 Copyright © 2004, Oracle. All rights reserved. Dependencies Table View Database trigger Procedure Function Package body Package specification User-defined object and collection types Function Package specification Procedure Sequence Synonym Table View User-defined object and collection types Referenced objectsDependent objects

1-32 Copyright © 2004, Oracle. All rights reserved. Dependencies View or procedure Direct dependency Referenced Indirect dependency Direct dependency Dependent Table Referenced xxxxxxxxxxxxxx vvvvvvvvvvvvvv xxxxxxxxxxxxxx vvvvvvvvvvvvvv xxxxxxxxxxxxxx vvvvvvvvvvvvvv xxxxxxxxxxxxxx vvvvvvvvvvvvvv xxxxxxxxxxxxxx vvvvvvvvvvvvvv Procedure Dependent

1-33 Copyright © 2004, Oracle. All rights reserved. Displaying Direct and Indirect Dependencies 1.Run the utldtree.sql script to create the objects that enable you to display the direct and indirect dependencies. 2.Execute the DEPTREE_FILL procedure: EXECUTE deptree_fill('TABLE','OE','CUSTOMERS')

1-34 Copyright © 2004, Oracle. All rights reserved. Using Oracle-Supplied Packages Oracle-supplied packages: Are provided with the Oracle server Extend the functionality of the database Enable access to certain SQL features that are normally restricted for PL/SQL For example, the DBMS_OUTPUT package was originally designed to debug PL/SQL programs.

1-35 Copyright © 2004, Oracle. All rights reserved. Some of the Oracle Supplied Packages Here is an abbreviated list of some Oracle-supplied packages: DBMS_ALERT DBMS_LOCK DBMS_SESSION DBMS_OUTPUT HTP UTL_FILE UTL_MAIL DBMS_SCHEDULER

1-36 Copyright © 2004, Oracle. All rights reserved. DBMS_OUTPUT Package The DBMS_OUTPUT package enables you to send messages from stored subprograms and triggers. PUT and PUT_LINE place text in the buffer. GET_LINE and GET_LINES read the buffer. Use SET SERVEROUTPUT ON to display messages in SQL*Plus. PUT_LINE GET_LINE PUT NEW_LINE GET_LINES SET SERVEROUT ON [SIZE n] EXEC proc Buffer Output

1-37 Copyright © 2004, Oracle. All rights reserved. UTL_FILE Package The UTL_FILE package extends PL/SQL programs to read and write operating system text files. UTL_FILE : Provides a restricted version of operating system stream file I/O for text files Can access files in operating system directories defined by a CREATE DIRECTORY statement. You can also use the utl_file_dir database parameter. EXEC proc O/S file UTL_FILE CREATE DIRECTORY my_dir AS '/dir'

1-38 Copyright © 2004, Oracle. All rights reserved. Summary In this lesson, you should have learned how to: Identify a PL/SQL block Create subprograms List restrictions and guidelines on calling functions from SQL expressions Use cursors Handle exceptions Use the raise_application_error procedure Identify Oracle-supplied packages

1-39 Copyright © 2004, Oracle. All rights reserved. Quiz Page 1

1-40 Copyright © 2004, Oracle. All rights reserved. Quiz Page 2

1-41 Copyright © 2004, Oracle. All rights reserved. Quiz Page 3

1-42 Copyright © 2004, Oracle. All rights reserved. Quiz Page 4