1 Preface
some global variables or constants are often encountered during the implementation of a program. This variable or constant is often stored in the global variables of the temporary table or foreground program during program development, resulting in reduced operational efficiency < Frequent read of temporary tables > or security pitfalls < stored in foreground program variables to track memory variables for >.
This paper mainly discusses the advantages and implementation methods of the global variable or constant access package.
2 Advantages
2.1 Execution efficiency is higher than temporary table, no frequency access temporary table required
2.2 Hiding the global variables in the last line of defense < database; High Security
2.3 You can invoke the variables of a package in a view to implement a dynamic view
3 Implementation
3.1 Overview of implementation methods
variables in the Oracle database package can be referenced directly in this package, but outside of the package, they cannot be referenced directly. Access to package variables can be implemented with corresponding stored procedures for each variable < for storing data > and functions < for reading data >.
3.2 Example
--Define package
Create or Replace package Pkg_system_constant is
c_systemtitle nVarChar2 (100): = ' Test global program variable ';--Define Constants
--Get constants < system titles >
Function Fn_getsystemtitle
Return nVarChar2;
g_currentdate date:=sysdate;--Define global variables
--Get global variables < current date >
Function fn_getcurrentdate
Return Date;
--Set global variables < current date >
Procedure sp_setcurrentdate
(p_currentdate in Date);
End pkg_system_constant;
/
Create or Replace package body Pkg_system_constant is
--Get constants < system titles >
Function Fn_getsystemtitle
Return nVarChar2
is
Begin
Return C_systemtitle;
End Fn_getsystemtitle;
--Get global variables < current date >
Function fn_getcurrentdate
Return Date
is
Begin
Return g_currentdate;
End fn_getcurrentdate;
--Set global variables < current date >
Procedure sp_setcurrentdate
(p_currentdate in Date)
is
Begin
g_currentdate:=p_currentdate;
End sp_setcurrentdate;
End pkg_system_constant;
/
3.3 Testing
--Test read Constants
Select pkg_system_constant.fn_getsystemtitle from Dual;
--Test setting global variables
Declare
Begin
pkg_system_constant.sp_setcurrentdate (to_date (' 2001.01.01 ', ' yyyy.mm.dd '));
End;
/
--Test read global variables
Select pkg_system_constant.fn_getcurrentdate from Dual;
====================
I heard: The global variable of package is only useful for one session, isn't it??? Pending verification.