You are on page 1of 3

27. 10. 8.

Creating Packages and call its functions

After the specification is created, you create the body of the package. The body of a package is a collection of schema objects that was declared in the specification. If you perform any initialization in the package body, it is executed once when the package is initially re To reference the package's subprograms and objects, you must use dot notation. The Syntax for Dot Notation
package_name.type_name package_name.object_name package_name.subprogram_name SQL> SQL> 2 3 4 5 6 7 8 9 10 11 -- create demo table create table Employee( ID VARCHAR2(4 BYTE) First_Name VARCHAR2(10 BYTE), Last_Name VARCHAR2(10 BYTE), Start_Date DATE, End_Date DATE, Salary Number(8,2), City VARCHAR2(10 BYTE), Description VARCHAR2(15 BYTE) ) /

NOT NULL,

Table created.

SQL> SQL> -- prepare data SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_ 2 values ('01','Jason', 'Martin', to_date('19960725','YYYYMMDD'), to_d 3 / 1 row created.

SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_ 2 values('02','Alison', 'Mathews', to_date('19760321','YYYYMMDD'), to_d 3 / 1 row created.

SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_ 2 values('03','James', 'Smith', to_date('19781212','YYYYMMDD'), to_d 3 / 1 row created.

SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_ 2 values('04','Celia', 'Rice', to_date('19821024','YYYYMMDD'), to_d 3 / 1 row created. SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date,

End_

2 3

values('05','Robert',

'Black',

to_date('19840115','YYYYMMDD'), to_d

1 row created.

SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_ 2 values('06','Linda', 'Green', to_date('19870730','YYYYMMDD'), to_d 3 / 1 row created.

SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_ 2 values('07','David', 'Larry', to_date('19901231','YYYYMMDD'), to_d 3 / 1 row created.

SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_ 2 values('08','James', 'Cat', to_date('19960917','YYYYMMDD'), to_d 3 / 1 row created. SQL> SQL> SQL> SQL> -- display data in the table SQL> select * from Employee 2 / ID ---01 02 03 04 05 06 07 08 FIRST_NAME ---------Jason Alison James Celia Robert Linda David James LAST_NAME ---------Martin Mathews Smith Rice Black Green Larry Cat START_DAT --------25-JUL-96 21-MAR-76 12-DEC-78 24-OCT-82 15-JAN-84 30-JUL-87 31-DEC-90 17-SEP-96 END_DATE SALARY CITY --------- ---------- ---------25-JUL-06 1234.56 Toronto 21-FEB-86 6661.78 Vancouver 15-MAR-90 6544.78 Vancouver 21-APR-99 2344.78 Vancouver 08-AUG-98 2334.78 Vancouver 04-JAN-96 4322.78 New York 12-FEB-98 7897.78 New York 15-APR-02 1232.78 Vancouver DESCRIPTION --------------Programmer Tester Tester Manager Tester Tester Manager Tester

8 rows selected. SQL> SQL> SQL> SQL> SQL> CREATE OR REPLACE PACKAGE inv_pck_spec as 2 FUNCTION inv_count(qty integer) RETURN integer; 3 PROCEDURE inv_adjust(qty integer); 4 END inv_pck_spec; 5 / Package created. SQL>

SQL> CREATE OR REPLACE PACKAGE BODY inv_pck_spec is 2 FUNCTION inv_count(qty integer)RETURN integer is 3 new_qty integer; 4 BEGIN 5 new_qty:= qty*6; 6 INSERT into employee (id,salary) values ('01',new_qty); 7 RETURN(new_qty); 8 END inv_count; 9 10 PROCEDURE inv_adjust(qty integer) is 11 BEGIN 12 DELETE from employee WHERE salary<qty; 13 END; 14 BEGIN -- package initialization begins here 15 INSERT into employee (id, first_name)values('01', 'new'); 16 17 END inv_pck_spec; 18 / Package body created. SQL> SQL> --call inv_pck_spec.inv_count(2); SQL> SQL> call inv_pck_spec.inv_adjust(2000); Call completed. SQL> SQL> select * from employee; ID ---02 03 04 05 06 07 01 FIRST_NAME ---------Alison James Celia Robert Linda David new LAST_NAME ---------Mathews Smith Rice Black Green Larry START_DAT --------21-MAR-76 12-DEC-78 24-OCT-82 15-JAN-84 30-JUL-87 31-DEC-90 END_DATE SALARY CITY --------- ---------- ---------21-FEB-86 6661.78 Vancouver 15-MAR-90 6544.78 Vancouver 21-APR-99 2344.78 Vancouver 08-AUG-98 2334.78 Vancouver 04-JAN-96 4322.78 New York 12-FEB-98 7897.78 New York DESCRIPTION --------------Tester Tester Manager Tester Tester Manager

7 rows selected. SQL> SQL> SQL> SQL> -- clean the table SQL> drop table Employee 2 / Table dropped. SQL> SQL>

You might also like