Advantages and disadvantages of "top" global variables

Source: Internet
Author: User

In the process of doing the project recently, the use of plsql is very much, mainly to write the stored procedure to realize the business logic. But in the process of coding encountered very strange problems.

The problem is: Define a variable in the package header, Current_time: = Sysdate, and then use this defined variable in procedure to insert directly into the table. A very simple implementation of it.

But strangely, every time the insert into the table is somehow changing, sometimes it's right, sometimes it's an indeterminate time, and sometimes it's just like the last time, it's really a headache.

Let's take a look at the code snippet below.

Package Header:

  1. Create or Replace package pkg_life_aml_data is
  2. current_time Date: = Sysdate;
  3. procedure T_normal (i_start_date in varchar2,
  4. I_end_date in Varchar2,
  5. i_organ_id in t_company_organ.organ_id%type);


Package body (the code that takes only the most critical parts):

  1. Procedure T_normal (I_start_date in Varchar2,
  2. I_end_date in Varchar2,
  3. i_organ_id in t_company_organ.organ_id%type) is
  4. INSERT INTO t_su_data
  5. (Insert_time)
  6. Values
  7. (current_time);
  8. end;

Through the above code is not difficult to see, this is actually a very simple conding ... But it does have a problem. The problem is being in the position of this current_time definition, which is actually a global variable.

The global variable in Plsql is given a value when the package is initialized, and will not change in the same session, so this leads to the reason why the time of the insert is incorrect, but it is not clear how Oracle determines the start and end of a session, because testing , whose time is sometimes correct.

So, the correct way is to insert the data, directly use the sysdate, and not in the Baotou place to assign sysdate to Current_time, so whether in Java or in the Plsql global variables can not be used, then when can use it?

1. The simplest, it is a constant, it will never change, then the definition of global variables will be very useful.

2.package_b want to use a value or a variable in the package_a, then you can define a global variable current_time (not assigned, only defined) in Package_a, and assign it to the stored procedure, and then switch to Package_b. Then in Package_b only need to call Package_a.current_time, you can get the value of the variable in package_a, and then do some operations in package_b based on this value. This is very useful.

To summarize, do not define variables as global variables! It's too dangerous.

Advantages and disadvantages of "top" global variables

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.