0% found this document useful (0 votes)
71 views3 pages

Sample PLSQL Packages

The document describes creating a PL/SQL package to manage customer data stored in an Oracle database. It defines procedures to add, delete, and list customers. The package is created, along with its body. Test code is provided that uses the package procedures to add two customers, list all customers, delete one, and list customers again.

Uploaded by

sastrylanka_1980
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views3 pages

Sample PLSQL Packages

The document describes creating a PL/SQL package to manage customer data stored in an Oracle database. It defines procedures to add, delete, and list customers. The package is created, along with its body. Test code is provided that uses the package procedures to add two customers, list all customers, delete one, and list customers again.

Uploaded by

sastrylanka_1980
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Example:

The following program provides a more complete package. We will use the CUSTOMERS table stored in our
database with the following records:
Select * from customers;

+----+----------+-----+-----------+----------+
| ID | NAME

| AGE | ADDRESS

| SALARY

+----+----------+-----+-----------+----------+
|

1 | Ramesh

32 | Ahmedabad |

3000.00 |

2 | Khilan

25 | Delhi

3000.00 |

3 | kaushik

23 | Kota

3000.00 |

4 | Chaitali |

25 | Mumbai

7500.00 |

5 | Hardik

27 | Bhopal

9500.00 |

6 | Komal

22 | MP

5500.00 |

+----+----------+-----+-----------+----------+

THE PACKAGE SPECIFICATION:


CREATE OR REPLACE PACKAGE c_package AS
-- Adds a customer
PROCEDURE addCustomer(c_id
c_name
c_age

customers.id%type,

customers.name%type,
customers.age%type,

c_addr customers.address%type,
c_sal

customers.salary%type);

-- Removes a customer
PROCEDURE delCustomer(c_id

customers.id%TYPE);

--Lists all customers


PROCEDURE listCustomer;

END c_package;
/
When the above code is executed at SQL prompt, it creates the above package and displays the following result:
Package created.

CREATING THE PACKAGE BODY:


CREATE OR REPLACE PACKAGE BODY c_package AS
PROCEDURE addCustomer(c_id

customers.id%type,

c_name customers.name%type,
c_age

customers.age%type,

c_addr

customers.address%type,

c_sal

customers.salary%type)

IS
BEGIN
INSERT INTO customers (id,name,age,address,salary)
VALUES(c_id, c_name, c_age, c_addr, c_sal);
END addCustomer;

PROCEDURE delCustomer(c_id

customers.id%type) IS

BEGIN
DELETE FROM customers
WHERE id = c_id;
END delCustomer;

PROCEDURE listCustomer IS
CURSOR c_customers is
SELECT

name FROM customers;

TYPE c_list is TABLE OF customers.name%type;


name_list c_list := c_list();
counter integer :=0;
BEGIN
FOR n IN c_customers LOOP
counter := counter +1;
name_list.extend;
name_list(counter)

:= n.name;

dbms_output.put_line('Customer(' ||counter|| ')'||name_list(counter));


END LOOP;
END listCustomer;
END c_package;
/

Above example makes use of nested table which we will discuss in the next chapter. When the above code is
executed at SQL prompt, it produces the following result:
Package body created.

USING THE PACKAGE:


The following program uses the methods declared and defined in the package c_package.
DECLARE
code customers.id%type:= 8;
BEGIN
c_package.addcustomer(7, 'Rajnish', 25, 'Chennai', 3500);
c_package.addcustomer(8, 'Subham', 32, 'Delhi', 7500);
c_package.listcustomer;
c_package.delcustomer(code);
c_package.listcustomer;
END;
/
When the above code is executed at SQL prompt, it produces the following result:
Customer(1):
Customer(2):
Customer(3):
Customer(4):
Customer(5):
Customer(6):
Customer(7):
Customer(8):
Customer(1):
Customer(2):
Customer(3):
Customer(4):
Customer(5):
Customer(6):
Customer(7):

Ramesh
Khilan
kaushik
Chaitali
Hardik
Komal
Rajnish
Subham
Ramesh
Khilan
kaushik
Chaitali
Hardik
Komal
Rajnish

PL/SQL procedure successfully completed

You might also like