An Training Guide PL/SQL for An Training Guide PL/SQL for Beginners ... is

  • View
    0

  • Download
    0

Embed Size (px)

Text of An Training Guide PL/SQL for An Training Guide PL/SQL for Beginners ... is

  • An

    Training Guide

    PL/SQL for

    Beginners

    Supplement

  • Training Guide PL/SQL for Beginners - Supplement

    Copyright © 1999 2004 ApplTop Solutions Limited 2

    www.appltop.com info@appltop.com

    Supplement

    The supplement covers a couple of topics that are

    what you might call Advanced PL/SQL, the word

    Advanced is purely relative, so a more appropriate

    title may have been, "Some PL/SQL we didnt have

    time for".

    The PL/SQL for Beginners guide introduced you to

    PL/SQL, the topics covered were based on what you

    are most likely to come across in your early

    developments.

    I have picked the topics in this document based on

    the fact that you are likely to use them from a

    potential pot of hundreds of topics.

    The style in which the topics are presented is

    different to that of the training course. The main

    course is in a tutorial style, whereas these topics are

    much lighter in detail and by example.

  • Training Guide PL/SQL for Beginners - Supplement

    Copyright © 1999 2004 ApplTop Solutions Limited 3

    www.appltop.com info@appltop.com

    Topics Covered

    The following topics are covered:-

    • PL/SQL Records & Tables

    • Host File Access with UTL_FILE

  • Training Guide PL/SQL for Beginners - Supplement

    Copyright © 1999 2004 ApplTop Solutions Limited 4

    www.appltop.com info@appltop.com

    PL/SQL Records & Tables

  • Training Guide PL/SQL for Beginners - Supplement

    Copyright © 1999 2004 ApplTop Solutions Limited 5

    www.appltop.com info@appltop.com

    PL/SQL Records & Tables

    Records

    We have already seen PL/SQL records, remember

    the variables you created which used the %ROWTYPE

    keyword in their declaration; this is a record, for

    example, r_employee emp_cur%ROWTYPE;

    The above record r_employee is a composite

    datatype containing all the columns found in the

    cursor emp_cur. A record could also be created

    containing all the columns on a table:- r_employee emp%ROWTYPE;

    Each element of a record is referenced using dot

    notation, for example, to reference the ename

    element of the r_employee record you would use:- r_employee.ename.

    You are not restricted to creating records that are

    anchored to a cursor or table, you can define your

    own records with the TYPE statement.

  • Training Guide PL/SQL for Beginners - Supplement

    Copyright © 1999 2004 ApplTop Solutions Limited 6

    www.appltop.com info@appltop.com

    PL/SQL Records & Tables

    Records

    To create a record manually that contains employee

    information you might use the following:- TYPE t_emp_record IS RECORD ( enumber NUMBER , firstname VARCHAR2(20) , lastname VARCHAR2(20));

    At this point, no PL/SQL object exists, just the new

    datatype, so, we need to declare a variable of this

    type:- l_employee t_emp_record;

    This is no different to declaring a variable of any of

    the standard types. You can now use the new

    l_employee record with the dot notation to get at

    its elements, for example:- l_employee.enumber := 100; l_employee.firstname := 'Fred'; l_employee.lastname := 'Bloggs';

  • Training Guide PL/SQL for Beginners - Supplement

    Copyright © 1999 2004 ApplTop Solutions Limited 7

    www.appltop.com info@appltop.com

    PL/SQL Records & Tables

    Assigning Records

    You can assign one record to another so long as all

    the elements in both records are exactly the same,

    for example:- DECLARE TYPE t_record IS RECORD ( col1 NUMBER , col2 VARCHAR2(10) ); l_record1 t_record; l_record2 t_record; BEGIN l_record1.col1 := 10; l_record1.col2 := 'Test'; l_record2 := l_record1; END;

    Records can also be used as arguments in

    procedures and functions, for example:- CREATE OR REPLACE PROCEDURE print_dept

    (p_dept_rec dept%ROWTYPE) IS BEGIN DBMS_OUTPUT.put_line(p_dept_rec.deptno);

    DBMS_OUTPUT.put_line(p_dept_rec.deptno); DBMS_OUTPUT.put_line(p_dept_rec.deptno);

    END;

    The above procedure could be invoked with:- DECLARE

    l_rec dept%ROWTYPE; BEGIN

    SELECT * INTO l_rec FROM dept WHERE deptno = 10; print_dept(l_rec); END;

  • Training Guide PL/SQL for Beginners - Supplement

    Copyright © 1999 2004 ApplTop Solutions Limited 8

    www.appltop.com info@appltop.com

    PL/SQL Records & Tables

    Creating Tables

    A PL/SQL table is very similar to an array found in

    most third generation languages. Before a table can

    be used, you need to declare a new datatype that is

    of type table, you do this with the TYPE statement

    in a similar way as you create records. The syntax

    of TYPE in this case is:- TYPE table-name IS TABLE OF type INDEX BY BINARY_INTEGER;

    Where table-name can be any valid identifier and

    type is any valid datatype, including any new

    datatypes you have created, such as a record. So,

    to declare a table to hold the names of employees

    you might:- TYPE t_names IS TABLE OF emp.ename%TYPE INDEX BY BINARY_INTEGER;

    The INDEX BY specifies how the records on the

    table will be indexed, in most cases, this should be

    BINARY_INTEGER (though this is not required as of

    Oracle8)

  • Training Guide PL/SQL for Beginners - Supplement

    Copyright © 1999 2004 ApplTop Solutions Limited 9

    www.appltop.com info@appltop.com

    PL/SQL Records & Tables

    Tables

    Once your new table type is declared, you must

    declare a variable of the new type, for example:- names_tab t_names;

    The above code will create a new variable called

    names_tab of type t_names. You can now use the

    table names_tab.

    You access individual rows on a PL/SQL table by

    using a table index (reference/subscript), similar to

    an array subscript. The index should appear

    immediately after the table name, for example, to

    set the elements of record one in the names_tab

    table:- names_tab(1).empno := 10; names_tab(1).ename := 'Fred';

    The subscript can be any valid number that can be

    represented by a binary integer.

  • Training Guide PL/SQL for Beginners - Supplement

    Copyright © 1999 2004 ApplTop Solutions Limited 10

    www.appltop.com info@appltop.com

    PL/SQL Records & Tables

    Tables

    Here is another example of a less complex table,

    this time it is a table of a scalar datatype:- DECLARE TYPE t_names IS TABLE OF VARCHAR2(10) INDEX BY BINARY_INTEGER; names_tab t_names; BEGIN names_tab(-10) := 'Fred'; names_tab(0) := 'John'; names_tab(250) := 'Richard'; END;

    The table in memory will now look as follows:-

    Index Value -10 Fred 0 John 250 Richard

    Memory has only been allocated for 3 rows, this is

    very much unlike arrays. To set the value of the

    250'th row in an array, all elements preceding it

    must exist. Nor can an array have a negative

    subscript. PL/SQL tables grow dynamically in size as

    you create rows, very much like a database table.

  • Training Guide PL/SQL for Beginners - Supplement

    Copyright © 1999 2004 ApplTop Solutions Limited 11

    www.appltop.com info@appltop.com

    PL/SQL Records & Tables

    Tables

    If you attempt to reference a row that has not

    already been created in a PL/SQL table, then an

    exception is raised, for example:- DECLARE TYPE t_names IS TABLE OF VARCHAR2(10) INDEX BY BINARY_INTEGER; names_tab t_names; BEGIN names_tab(0) := 'Fred'; names_tab(1) := 'John'; names_tab(2) := 'Richard'; DBMS_OUTPUT.put_line(names_tab(3)); END;

    The above code will fail with the following error:- ORA-01403: no data found

    It will failed because in the line which displays a

    value (DBMS_OUTPUT), we are referencing a row

    which does not yet exist. You have to handle the

    error in a similar fashion as you would for a failing

    SELECT statement.

  • Traini