Packages in Oracle

Source: Internet
Author: User

Basic concepts of a package

A package can organize several functions or stored procedures and store them as an object. A package usually consists of two parts: specification and body ). The program reports can also contain constants and variables. All functions and stored procedures in the package can use these variables or constants.

Ii. Specification

1. Create a specification (SQL window)

Create or replace package pkg_staff
StaffString varchar2 (500 );
StafftAge number: = 18;
Function get_staff_string return varchar2;
Procedure insert_staff (in_staff_id in number, in_staff_name in varchar2 );
Procedure update_staff (in_staff_id in number );
Procedure delete_staff (in_staff_id in number );
End pkg_staff;


2. view the package specification information in the data dictionary

Select object_name, object_type, status from user_objects
Where lower (OBJECT_NAME) = 'pkg _ staff'

Three Subjects

The so-called specification is like an interface in object-oriented programming. The subject of this specification must implement all the methods of this specification. Oracle will automatically look for a specification with the same name as the subject to see if all the functions or stored procedures have been implemented. If no, the compilation is incorrect.

1. Create a subject

Create or replace package body pkg_staff
Function get_staff_string return varchar2
Begin
Return 'staff ';
End get_staff_string;
Procedure insert_staff (in_staff_id in number, in_staff_name in varchar2)
Begin
Insert into staff values (in_staff_id, in_staff_name );
End insert_staff;
Procedure update_staff (in_staff_id in number)
Begin
Update staff set name = 'xy' where num = in_staff_id;
End update_staff;
Procedure delete_staff (in_staff_id in number)
Begin
Delete from staff where num = '1 ';
End delete_staff;
End pkg_staff;

2. view the information of the package owner in the data dictionary.

Select object_name, object_type, status from user_objects
Where lower (OBJECT_NAME) = 'pkg _ staff'

4. Call functions or stored procedures in the package

Call a function (SQL window)
Select pkg_staff.get_staff_string () as result from dual


Call a stored procedure (Command window)
Begin
Pkg_staff.delete_staff (1 );
End;
/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.