s09l05_using oracle-supplied packages

Upload: degel-aresen-argent

Post on 03-Apr-2018

219 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/28/2019 S09L05_Using Oracle-Supplied Packages

    1/23

    Using Oracle-SuppliedPackages

  • 7/28/2019 S09L05_Using Oracle-Supplied Packages

    2/23

    2home back first prev next last

    What Will I Learn?

    Describe two common uses for theDBMS_OUTPUT server-supplied package

    Recognize the correct syntax to specify

    messages for the DBMS_OUTPUT package Describe the purpose for the UTL_FILE server-

    supplied package.

    Recall the exceptions used in conjunction with

    the UTL_FILE server-supplied package.

  • 7/28/2019 S09L05_Using Oracle-Supplied Packages

    3/23

    3home back first prev next last

    Why Learn It?

    You already know that Oracle supplies a number of SQLfunctions (UPPER, TO_CHAR and so on) which we canuse in our SQL statements when required. It would bewasteful for everyone to have to re-invent the wheel bywriting their own functions to do these things.

    In the same way, Oracle supplies a number of ready-made PL/SQL packages to do things which mostapplication developers and/or database administrators

    will need to do from time to time.

    In this lesson, you learn how to use two of the Oracle-supplied PL/SQL packages, which focus on generatingtext output and manipulating text files.

  • 7/28/2019 S09L05_Using Oracle-Supplied Packages

    4/23

    4home back first prev next last

    Using Oracle-Supplied Packages

    You can use these packages directly by invokingthem from your own application, exactly as youwould invoke packages which you had writtenyourself.

    Or, you may simply want to use these packagesas ideas when you create your ownsubprograms.

    Think of these packages as ready-madebuilding blocks which you can invoke from yourown applications.

  • 7/28/2019 S09L05_Using Oracle-Supplied Packages

    5/23

    5home back first prev next last

    List of Some Oracle-Supplied Packages

  • 7/28/2019 S09L05_Using Oracle-Supplied Packages

    6/23

    6home back first prev next last

    The DBMS_OUTPUT Package

    The DBMS_OUTPUT package sends textmessages from any PL/SQL block into aprivate memory area, from which themessage can be displayed on the screen.

    Common uses of DBMS_OUTPUT include:

    You can output results back to the developerduring testing for debugging purposes.

    You can trace the code execution path for afunction or procedure.

  • 7/28/2019 S09L05_Using Oracle-Supplied Packages

    7/237home back first prev next last

    How the DBMS_OUTPUT Package Works

    The DBMS_OUTPUT package enables you tosend messages from stored subprograms andanonymous blocks. PUT places text in the buffer.

    NEW_LINE sends the buffer to the screen. PUT_LINE does a PUT followed by a NEW_LINE.

    GET_LINE and GET_LINES read the buffer.

    Messages are not sent until after the calling block

    finishes.

  • 7/28/2019 S09L05_Using Oracle-Supplied Packages

    8/238home back first prev next last

    Using DBMS_OUTPUT: Example 1

    You have already usedDBMS_OUTPUT.PUT_LINE. This writes a

    text message into a buffer, then displays

    the buffer on the screen:

    If we wanted to build a message a little ata time, we could code:

  • 7/28/2019 S09L05_Using Oracle-Supplied Packages

    9/239home back first prev next last

    Using DBMS_OUTPUT: Example 2

    We can trace the flow of execution of ablock with complex IF ELSE, CASE or

    looping structures:

  • 7/28/2019 S09L05_Using Oracle-Supplied Packages

    10/2310home back first prev next last

    DBMS_OUTPUT

    is designed for debugging only

    We would not use DBMS_OUTPUT in PL/SQLprograms which are called from a real

    application, which can include its own application

    code to display results on the users screen.

    Instead, we would return the text to be displayed

    as an OUT argument from the subprogram. For

    example:

    Would be converted to:

  • 7/28/2019 S09L05_Using Oracle-Supplied Packages

    11/2311home back first prev next last

    DBMS_OUTPUT

    is designed for debugging only

    For this reason, you should not useDBMS_OUTPUT in subprograms, but only in

    anonymous PL/SQL blocks for testing purposes.

    Instead of:

    You should use:

  • 7/28/2019 S09L05_Using Oracle-Supplied Packages

    12/2312home back first prev next last

    The UTL_FILE package

    Allows PL/SQL programs to read and writeoperating system text files

    Can access text files in operating systemdirectories defined by a CREATE DIRECTORY

    statement. You can also use the utl_file_dirdatabase parameter.

  • 7/28/2019 S09L05_Using Oracle-Supplied Packages

    13/2313home back first prev next last

    File Processing

    Using the UTL_FILE Package

  • 7/28/2019 S09L05_Using Oracle-Supplied Packages

    14/2314home back first prev next last

    File Processing

    Using the UTL_FILE Package

    You open files for reading or writing with the FOPENfunction. You then either read from or write or append tothe file until processing is done. Then close the file byusing the FCLOSE procedure. The following are the mainsubprograms:

    The FOPEN function opens a file in a specified directory forinput/output (I/O) and returns a file handle used in later I/Ooperations.

    The IS_OPEN function checks if the file is already open,returning a Boolean.

    The GET_LINE procedure reads a line of text from the fileinto an output buffer parameter. The maximum input recordsize is 1,023 bytes.

    The PUT and PUT_LINE procedures write text to the openedfile.

    The NEW_LINE procedure terminates a line in an output file.

    The FCLOSE procedure closes an opened file.

  • 7/28/2019 S09L05_Using Oracle-Supplied Packages

    15/2315home back first prev next last

    Exceptions in the UTL_FILE Package

    You may have to handle one or more of theseexceptions when using UTL_FILE subprograms: INVALID_PATH

    INVALID_MODE

    INVALID_FILEHANDLE INVALID_OPERATION

    READ_ERROR

    WRITE_ERROR

    INTERNAL_ERROR

    The other exceptions not specific to theUTL_FILE package are: NO_DATA_FOUND and VALUE_ERROR

  • 7/28/2019 S09L05_Using Oracle-Supplied Packages

    16/2316home back first prev next last

    FOPEN and IS_OPEN Function Parameters

    Example:

  • 7/28/2019 S09L05_Using Oracle-Supplied Packages

    17/2317home back first prev next last

    Using UTL_FILE: Example

    In this example, the sal_status procedure usesUTL_FILE to create a text report of employees for eachdepartment, along with their salaries. In the code, thevariable v_file is declared as UTL_FILE.FILE_TYPE, aBINARY_INTEGER datatype that is declared globally by

    the UTL_FILE package.

    The sal_status procedure accepts two IN parameters:p_dir for the name of the directory in which to write the

    text file, and p_filename to specify the name of the file.

    To invoke the procedure, use (for example):

  • 7/28/2019 S09L05_Using Oracle-Supplied Packages

    18/2318home back first prev next last

    Using UTL_FILE: Example

  • 7/28/2019 S09L05_Using Oracle-Supplied Packages

    19/2319home back first prev next last

    Using UTL_FILE: Example

  • 7/28/2019 S09L05_Using Oracle-Supplied Packages

    20/2320home back first prev next last

    Using UTL_FILE:

    Example : Invocation and Output Report

    Suppose we invoke our procedure by:

    The output contained in the file will be:

  • 7/28/2019 S09L05_Using Oracle-Supplied Packages

    21/2321home back first prev next last

    Terminology

    Key terms used in this lesson include: DBMS_OUTPUT

    UTL_FILE package

  • 7/28/2019 S09L05_Using Oracle-Supplied Packages

    22/23

    22home back first prev next last

    Summary

    In this lesson, you learned to: Describe two common uses for the

    DBMS_OUTPUT server-supplied package

    Recognize the correct syntax to specifymessages for the DBMS_OUTPUT package

    Describe the purpose for the UTL_FILE

    server-supplied package.

    Recall the exceptions used in conjunction with

    the UTL_FILE server-supplied package.

  • 7/28/2019 S09L05_Using Oracle-Supplied Packages

    23/23

    23home back first prev next last

    Try It / Solve It

    The exercises in this lesson cover thefollowing topics:

    Describing two common uses for theDBMS_OUTPUT server-supplied package

    Recognizing the correct syntax to specifymessages for the DBMS_OUTPUT package

    Describing the purpose for the UTL_FILE

    server-supplied package. Recalling the exceptions used in conjunction

    with the UTL_FILE server-supplied package.