Brief analysis of package
Packages in Oracle Brief analysis of the basic concept of a package
Packages can organize several functions or stored procedures to be stored as an object. The package is usually composed of two parts, specification (specification) and body (body).
Packages can also contain constants and variables, and all functions and stored procedures in a package can use these variables or constants.
A package is an encapsulation package of related procedures, functions, variables, cursors, and exceptions, consisting of two parts, the specification and the main body.
Two specifications
1 Creating a Specification (SQL window)
Create or replace package Pkg_staff as
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 information about package specifications in a data dictionary
Select Object_name,object_type,status from User_objects
where lower (object_name) = ' Pkg_staff '
Three main body
A specification, like an interface in object-oriented programming, must implement all the methods of the specification. Oracle automatically looks for specifications with the same name as the principal to see if all of the canonical functions or stored procedures are implemented. If not, compile the error.
1 Creating principals
Create or replace package body Pkg_staff as
function get_staff_string return VARCHAR2 as
Begin
Return ' staff ';
End get_staff_string;
Procedure Insert_staff (in_staff_id in Number,in_staff_name in varchar2) as
Begin
INSERT into staff values (in_staff_id,in_staff_name);
End Insert_staff;
Procedure Update_staff (in_staff_id in number) as
Begin
Update staff Set name = ' xy ' where num = in_staff_id;
End Update_staff;
Procedure Delete_staff (in_staff_id in number) as
Begin
Delete from the staff where num = ' 1 ';
End Delete_staff;
End Pkg_staff;
2 Viewing the information of the package body in a data dictionary
Select Object_name,object_type,status from User_objects
where lower (object_name) = ' Pkg_staff '
Www.2cto.com
Four calls to a function or stored procedure in a package
Call function (SQL window)
Select Pkg_staff.get_staff_string () as result from dual
Calling a stored procedure (Command window)
Begin
Pkg_staff.delete_staff (1);
End
/
Oracle Package 1-The base of the package