Sie sind auf Seite 1von 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 procedure studentupdate is a procedure studentdelete is a procedure studentcount is a procedure studentsearch is a procedure Instead of creating several individual procedures, we create a package. When compared to procedures packages have some additional advantages: Information hiding Performance 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/O 2. Then it executes called procedure. What can we define inside a package? 1. Procedures 2. Functions 3. Variables 4. 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.

-- 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. <packagename>.<membername>; First we have to declare package specification Then create package body. Syntax: create or replace package<packagename> IS variablename datatype; -- normal variable declare syntax procedure <procedurename>(withparameters); function <functionname>(withparameters) return <datatype>; ........................................................................ end; Creating Package body: create or replace package body <packagename> is -- Code for all procedures and functions is written here. -- Specified in pakage specification. procedure <procedurename> (withparameters) is begin exception end; fucntion<functionname>(withparameters) return <type> is

begin end; .............................................. end; Package for implementing airthmetic operations: add2values sub2values 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 number is 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 declare c number(4); begin dbms_output.put_line(mathoperations.msg); -- acessing packge varibale1 begin dbms_output.put_line(mathoperation.msg); c:=mathoperation.add2values (3,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<packagename>; -- drops only body drop pakcage<packagename> -- 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.

Das könnte Ihnen auch gefallen