Oracle Part3

download Oracle Part3

of 106

  • date post

  • Category


  • view

  • download


Embed Size (px)

Transcript of Oracle Part3

  • 7/29/2019 Oracle Part3


    Chapter 4:

    Introduction to PL/SQL

    Guide to Oracle 10g

  • 7/29/2019 Oracle Part3


    Guide to Oracle 10g 2

    Lesson A Objectives

    After completing this lesson, you should be able to:

    Describe the fundamentals of the PL/SQL

    programming language Write and execute PL/SQL programs in SQL*Plus

    Execute PL/SQL data type conversion functions

    Display output through PL/SQL programs

    Manipulate character strings in PL/SQL programs

    Debug PL/SQL programs

  • 7/29/2019 Oracle Part3


    Guide to Oracle 10g 3


    DB users use GUI applications to enter data intoinput boxes, and then click buttons to executeprograms that perform DB functions.

    We will use the following utilities:Form builder

    Report builder

    To use these utilities effectively an understanding ofPL/SQL is needed.

    PL/SQL is a procedural programming language thatOracle utilities use to manipulate DB data.

    Programming or scripting language conceptsunderstanding is necessary.

  • 7/29/2019 Oracle Part3


    Guide to Oracle 10g 4

    Fundamentals of PL/SQL

    PL/SQL program combines SQL queries withprocedural commands (if/then, loops).

    It is a full-featured programming language Executed using Oracle 10gutilities such as:SQL*Plus or

    Forms Builder It is an interpreted language

    Its commands are not a case sensitive. Inside single

    quotes are case sensitive.. M != m Semicolon ends each command

    It contains reserved words and built in functions.

  • 7/29/2019 Oracle Part3


    Guide to Oracle 10g 5

    PL/SQL Command Capitalization


  • 7/29/2019 Oracle Part3


    Guide to Oracle 10g 6

    PL/SQL Variables and Data Types

    Variable names must follow the Oracle namingstandard

    Strongly typed language:Explicitly declare each variable including data type

    before using variable

    Variable declaration syntax: var i abl e_name dat a_t ype_decl ar at i on;

    Default value always NULL PL/SQL supports: scalar, composite, reference and


  • 7/29/2019 Oracle Part3


    Guide to Oracle 10g 7

    Scalar Variables

    Reference single value Data types correspond to Oracle 10gdatabase data





  • 7/29/2019 Oracle Part3


    Guide to Oracle 10g 8

    General Scalar Data Types

  • 7/29/2019 Oracle Part3


    Guide to Oracle 10g 9

    Composite Variables

    Data Structure is a data object made up of multiple

    individual data elements Composite variable is a data structure contains

    multiple scalar variables




  • 7/29/2019 Oracle Part3


    Guide to Oracle 10g 10

    Reference Variables

    Reference variables directly reference specificdatabase column or row

    Reference variables assume data type of associatedcolumn or row

    %TYPE data declaration syntax: var i abl e_namet abl ename. f i el dname%TYPE;

    %ROWTYPE data declaration syntax: var i abl e_name t abl ename%ROWTYPE;

  • 7/29/2019 Oracle Part3


    Guide to Oracle 10g 11

    Reference Variables

    The (%TYPE) reference data type specifies avariable that references a single DB field.

    current_f_last FACULTY.F_LAST%TYPE;

    The current_f_last variable assumes a data type ofVARCHAR2(30), because this is the data type ofthe f_last column in the FACULTY table.

  • 7/29/2019 Oracle Part3


    Guide to Oracle 10g 12

    Reference Variables

    The (%ROWTYPE) reference data type createscomposite variables that reference the entire data

    record. Facul t y_r ow FACULTY%ROWTYPE;

    The variable faculty_row references all of thecolumns in the FACULTY table, and each column

    has the same data type as its associated DB column.

  • 7/29/2019 Oracle Part3


    Guide to Oracle 10g 13

    LOB Data Types

    It declares variables that referencebinary orcharacterdata objects up to 4 GB.

    LOB values in PL/SQL programs must be

    manipulated using special package calledDBMS_LOB.

  • 7/29/2019 Oracle Part3


    Guide to Oracle 10g 14

    PL/SQL Program Blocks

    Declaration section (DECLARE) Optional (variable declaration)

    Execution section (BEGIN) Required (Program statements)

    Exception section (EXCEPTION) Optional (Error handling statements)

    Comment statements

    Enclosed within /* and */ Two hyphens (--) for a single line comment


  • 7/29/2019 Oracle Part3


    Guide to Oracle 10g 15

    PL/SQL Arithmetic Operators in

    Describing Order of Precedence

  • 7/29/2019 Oracle Part3


    Guide to Oracle 10g 16

    Assignment Statements

    Assigns value to variable Operator


    Syntax var i abl e_name : = val ue;

    String literal must be enclosed in a singlequotation marks.

  • 7/29/2019 Oracle Part3


    Guide to Oracle 10g 17

    Assignment Statements You can use the := in the declaration section to

    give initial values. s_id NUMBER := 100;

    You can use DEFAULT instead: s_id NUMBER DEFAULT 100;

    IfNULL value is used in an assignment statementwith arithmetic operation, the result is always


  • 7/29/2019 Oracle Part3


    Guide to Oracle 10g 18

    Executing a PL/SQL Program in


    Create and execute PL/SQL program blockswithin variety of Oracle 10gdevelopmentenvironments:

    SQL *PlusForm Builder

    Report Builder

  • 7/29/2019 Oracle Part3


    Guide to Oracle 10g 19

    Displaying PL/SQL Program Output in SQL*Plus

    PL/SQL outputbuffer

    Memory area on database server

    Stores programs output values before they aredisplayed to user

    Buffer size should be increased before output is

    displayed: SET SERVEROUTPUT ON SI ZE buf f er _si ze

    Default buffer size

    2000 bytes

    If the output is more than buffer_size, error apperas.

  • 7/29/2019 Oracle Part3


    Guide to Oracle 10g 20

    Displaying PL/SQL Program Output in SQL*Plus


    Display program output

    DBMS_OUTPUT. PUT_LI NE( ' di spl ay_t ext' ) ;

    Display maximum of 255 characters of text data.

    More than 255 characters gives an error.

    Example: s_name := Ahmed Mahmoud;

    DBMS_OUTPUT. PUT_LI NE( s_name) ;

  • 7/29/2019 Oracle Part3


    Guide to Oracle 10g 21

    Writing a PL/SQL Program

    Write PL/SQL program in Notepad or another texteditor

    Copy and paste program commands into SQL*Plus

    Press Enter after last program command Type front slash ( / ) (to instruct the interpreter to execute the code)

    Then press Enter again

  • 7/29/2019 Oracle Part3


    Guide to Oracle 10g 22

    Writing a PL/SQL Program


    Good programming practicePlace DECLARE, BEGIN, and END commandsflush with left edge of text editor window

    Indent commands within each section

  • 7/29/2019 Oracle Part3


    Guide to Oracle 10g 23

    PL/SQL Program Commands

    Output: Todays data is

    18-OCT-07PL/SQL procedure successfully completed

    If no output appears use: SQL> set serveroutput on

  • 7/29/2019 Oracle Part3


    Guide to Oracle 10g 24

    PL/SQL Data Conversion Functions

    Implicit data conversions Interpreter automatically converts value from one data

    type to another(the command DBMS_OUTPUT.PUT_LINE(todays_date), theinterpreter automatically converted the todas_date DATE variable to a text string foroutput

    ). If PL/SQL interpreter unable to implicitly convert value

    error occurs

    Explicit data conversionsConvert variables to different data typesUsing built in data conversion functions

  • 7/29/2019 Oracle Part3


    Guide to Oracle 10g 25

    PL/SQL Data Conversion Functions of


  • 7/29/2019 Oracle Part3


    Guide to Oracle 10g 26

    Manipulating Character Strings with

    PL/SQL String

    Character data value

    Consists of one or more characters


    Joining two separate strings Parse

    Separate single string consisting of two data items

    separated by commas or spaces Built in functions to concatenate and parse strings

    in SQL*Plus are as follows:

  • 7/29/2019 Oracle Part3


    Guide to Oracle 10g 27

    Concatenating Character Strings

    Double bar Operator



    new_st r i ng : = st r i ng1 | | st r i ng2;

    Exampl e: Ful l _name : = Qusai | | | | Abuei n

    You must conver t NUMBER and DATE dat at ype var i abl es ( TO_CHAR) i nt o a st r i ngbef or e concat enat i ng i t .

  • 7/29/2019 Oracle Part3


    Guide to Oracle 10g 28

    Removing Blank Leading and Trailing

    Spaces from Strings

    LTRIM functionRemove blank leading spaces

    st r i ng : = LTRI M( st r i ng_var i abl e_name) ;

    RTRIM functionRemove blank trailing spaces

    st r i ng : = RTRI M( st r i ng_var i abl e_name) ;

  • 7/29/2019 Oracle Part3


    Guide to Oracle 10g 29

    Finding the Length of Character


    LENGTH function syntax

    st r i ng_l engt h :