In Oracle Forms, global variables are one of the simplest yet powerful features used to share data between forms, triggers, and program units. They allow developers to define values that can be accessed anywhere in the application, making them extremely useful when working with multiple forms or passing information across different parts of the system.
This article will explain what global variables are in Oracle Forms, how they work, how to set and retrieve their values, and the best practices to follow when using them.
What are Global Variables in Oracle Forms?
A global variable is a memory location that stores a value, which can then be accessed from anywhere within the Oracle Forms session. Unlike local variables, which exist only within a trigger or procedure, global variables remain available throughout the lifetime of the form session until they are explicitly cleared.
Key Characteristics of Global Variables:
- Declared and managed using Oracle Forms built-ins.
- Accessible across different forms and program units.
- Exist for the duration of the session unless deleted.
- Can hold character, numeric, or date values.
- Useful for passing values from one form to another.
How to Define and Use Global Variables
Oracle Forms provides three built-ins to work with global variables:
NAME_IN– Retrieves the value of a global variable.COPY– Assigns a value to a global variable.ERASE– Deletes a global variable from memory.
Step 1: Assign a Value to a Global Variable
Use the COPY built-in to set a value:
COPY('Admin', 'GLOBAL.user_role');
- Here, the global variable
GLOBAL.user_roleis assigned the value'Admin'. - The prefix
GLOBAL.is mandatory for global variables.
Step 2: Retrieve a Value from a Global Variable
Use the NAME_IN built-in to read the value:
DECLARE
v_role VARCHAR2(20);
BEGIN
v_role := NAME_IN('GLOBAL.user_role');
MESSAGE('Current Role: ' || v_role);
MESSAGE(' ');
END;
- This retrieves the value stored in
GLOBAL.user_roleand displays it.
Step 3: Delete a Global Variable
Use the ERASE built-in when the variable is no longer needed:
ERASE('GLOBAL.user_role');
- This frees up memory and prevents old values from affecting future sessions.
Example: Passing Data Between Forms
One common use of global variables is to transfer values from one form to another.
In Form A (Setting a value):
COPY(:emp.emp_id, 'GLOBAL.emp_id');
CALL_FORM('emp_details', NO_HIDE, DO_REPLACE);
In Form B (Retrieving the value):
:emp_details.emp_id := NAME_IN('GLOBAL.emp_id');
- Here, the employee ID from Form A is passed to Form B through the global variable.
Advantages of Using Global Variables
- Easy Data Sharing: Quickly pass values across different forms.
- No Extra Database Calls: Reduces the need to re-query values from the database.
- Session-Wide Scope: Variables remain accessible as long as the session is active.
- Simple to Implement: Only requires built-in functions without complex coding.
Best Practices for Global Variables
- Use Clear and Consistent Naming
- Always use descriptive names, e.g.,
GLOBAL.current_user,GLOBAL.session_id.
- Always use descriptive names, e.g.,
- Erase When Not Needed
- Free unused variables with
ERASEto prevent memory leaks.
- Free unused variables with
- Avoid Overuse
- Do not rely heavily on global variables for all logic. Instead, use them only for values that need to be shared across forms.
- Check Before Using
- Always ensure the variable has been initialized before calling
NAME_INto avoid runtime errors.
IF NOT ID_NULL('GLOBAL.user_role') THEN MESSAGE('Role: ' || NAME_IN('GLOBAL.user_role')); END IF; - Always ensure the variable has been initialized before calling
- Prefer Parameters for Local Use
- For logic inside a single form or program unit, use parameters or local variables instead of global variables.
Common Mistakes to Avoid
- Forgetting to Erase Variables: Old values may remain in memory and cause unexpected results.
- Using Too Many Globals: This makes debugging and maintenance harder.
- Assuming They Are Secure: Global variables are stored in memory, so avoid using them for highly sensitive information like passwords.
- Incorrect Naming: Not using the
GLOBAL.prefix leads to runtime errors.
Conclusion
Global variables in Oracle Forms are a simple but effective way to share data across different forms and program units. By using the COPY, NAME_IN, and ERASE built-ins, developers can easily assign, retrieve, and manage session-wide values. When used carefully and with proper best practices, global variables can significantly reduce redundant coding and improve the efficiency of Oracle Forms applications. However, they should be used wisely and erased when no longer needed to maintain application stability.

