Application of global variables of Oracle database package

Source: Internet
Author: User

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.

Related Article

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.