June 11th Monday

download June 11th Monday

of 4

Transcript of June 11th Monday

  • 7/31/2019 June 11th Monday

    1/4

    Packages:

    It is a schema object.

    It is a group of logically related procedures or functions or variable, cursor and types into a

    single unit.

    If STUDENT is a table:

    studentinsert is a procedurestudentupdate is a procedure

    studentdelete is a procedure

    studentcount is a procedurestudentsearch is a procedure

    Instead of creating several individual procedures, we create a package.

    When compared to procedures packages have some additional advantages:

    Information hidingPerformance Improvement

    pkgstudent

    Client is calling student insert procedure

    1. First it loads procedure from database into oracle SGA-- running copy -- reading from file to

    SGA -- I/O2. Then it executes called procedure.

    What can we define inside a package?

    1. Procedures

    2. Functions

    3. Variables4. Cursors & ref cursor

    5. Types

    6. Exceptions

    Parts of a Package:

    1. Package Specification-- Declaration of package members (procedures, function, variables...)

    -- That means individual syntax of procedures, functions....

    2. Package body -- Optional-- Contains code of package members.

  • 7/31/2019 June 11th Monday

    2/4

    -- Package body is not required, when package specification does not contain any procedures or

    functions.

    All members of package divided into two types:

    1. Public-- All members declared in specification are called public.

    -- All public members can be called from outside of package.

    2. Private-- All members declared in package body and not in specification are called private.

    -- Private members cannot be called from outside of package; it should be called from within

    package body.

    How can we call package members?

    using "." nototion.

    .;

    First we have to declare package specification

    Then create package body.

    Syntax:

    create or replace package

    ISvariablename datatype; -- normal variable declare syntax

    procedure (withparameters);

    function (withparameters) return ;

    ........................................................................end;

    Creating Package body:

    create or replace package body

    is-- Code for all procedures and functions is written here.

    -- Specified in pakage specification.

    procedure (withparameters)

    isbegin

    exception

    end;

    fucntion(withparameters) return

    is

  • 7/31/2019 June 11th Monday

    3/4

    begin

    end;

    ..............................................

    end;

    Package for implementing airthmetic operations:

    add2valuessub2values

    mul2values

    div2values

    create or replace package mathoperations

    is

    function add2values(a number, b number) return number;

    function sub2values(a number, b number) return number;function mul2vaues(a number, b number) return number;

    function div2values(a number, b number) return number;msg varchar2(100):='welcome';

    end;

    create or replace package body mathoperations

    is

    function add2values(a number, b number)

    return numberis

    res number(4);

    begin

    res:= a + b;return res;

    end;

    end;

    To find the errors:

    select * from all_errors where name='MATHOPERATORS'

    set serveroutput on

    declarec number(4);

    begin

    dbms_output.put_line(mathoperations.msg); -- acessing packge varibale1begin

    dbms_output.put_line(mathoperation.msg);

    c:=mathoperation.add2values (3,4);

  • 7/31/2019 June 11th Monday

    4/4

    dbms_output.put_line('sum is' || c);

    end;

    We can have two variables with same name in packages. Oracle recogizes depending on

    datatypes. The datatypes should not be the same.

    Dropping a package:

    drop package body; -- drops only bodydrop pakcage -- this drops entire package.

    HW ASSIGN:

    create a table called student

    id, firstname, lastname, age, gender

    Create a package called stgpackage with following members

    1. stdinsert -- for inserting a record into student table...Note: id should be generated automatically... No need to pass as parameters.

    2. stdupdate -- for updating a record. -- It should return status-- All parameters should be passed... Update is based on id

    3. Create a procedure for delete std based on id and based on name -- status should be returned.

    4. Display student... Either based on id or name.

    5. getstudentcount -- Funtion for getting std count...

    -- calling programs for testing each package members.