In Oracle Forms, developers often need to control how queries are executed on blocks and items. One of the most useful triggers for this purpose is the PRE-QUERY Trigger. This trigger allows you to modify or enhance query conditions dynamically before Oracle Forms actually fetches the data. In this article, you will learn what the PRE-QUERY Trigger is, how it works, why it is important, examples of its usage, and best practices for implementing it correctly.
What is the PRE-QUERY Trigger?
The PRE-QUERY Trigger is a block-level trigger in Oracle Forms. It fires immediately before a query is executed on a block. This gives developers the opportunity to customize the query by changing WHERE clause conditions or assigning values to block items before the query runs.
Key characteristics:
- It is defined at the block level.
- Fires before data is retrieved from the database.
- Allows developers to modify query conditions dynamically.
- Useful for adding security checks, default filters, or user-based restrictions.
Purpose of the PRE-QUERY Trigger
The main purpose of the PRE-QUERY Trigger is to control the type of data that is fetched by the query. Instead of relying solely on user-entered values, developers can add conditions or filters in the background.
Some common purposes include:
- Restricting data by user role (e.g., showing only employees of a specific department).
- Adding default conditions so users do not have to type them.
- Enhancing security by preventing access to sensitive data.
- Dynamically constructing WHERE clauses based on runtime variables.
- Improving performance by reducing the amount of unnecessary data fetched.
When Does the PRE-QUERY Trigger Fire?
The PRE-QUERY Trigger fires whenever a query is about to execute on a block. This can happen in the following scenarios:
- When the user presses the Execute Query button.
- When a query is executed programmatically using the
EXECUTE_QUERYbuilt-in. - When the block is entered and a query is triggered automatically (depending on form logic).
In every case, PRE-QUERY executes before the actual SQL SELECT statement runs against the database.
Difference Between PRE-QUERY and POST-QUERY
To avoid confusion, let us clarify the difference between the two commonly used triggers:
- PRE-QUERY Trigger: Fires before the query executes. Used to modify or add query conditions.
- POST-QUERY Trigger: Fires after each record is fetched. Used to perform additional actions on retrieved data, such as formatting or fetching related details.
Simply put:
- PRE-QUERY = Modify conditions before fetching data
- POST-QUERY = Take action after data is fetched
Example of PRE-QUERY Trigger
Here’s a practical example of how PRE-QUERY can be used:
BEGIN
-- Restrict data based on the current user’s department
IF :GLOBAL.DEPT_ID IS NOT NULL THEN
:EMP.DEPT_ID := :GLOBAL.DEPT_ID;
END IF;
-- Add a default filter for active employees only
:EMP.STATUS := 'ACTIVE';
END;
Explanation:
- The user’s department is taken from a global variable and automatically applied as a filter.
- The query is modified so that only employees with status
ACTIVEare retrieved. - The user does not need to manually enter these conditions.
Using PRE-QUERY with the ADDITIONAL WHERE Clause
Another advanced use of PRE-QUERY is dynamically appending WHERE clauses using the SET_BLOCK_PROPERTY built-in.
Example:
BEGIN
-- Add a custom WHERE condition to restrict salary range
SET_BLOCK_PROPERTY('EMP', DEFAULT_WHERE, 'SALARY > 3000 AND SALARY < 8000');
END;
Here, the default WHERE condition is set programmatically, and it will apply whenever a query is executed on the EMP block.
Best Practices for PRE-QUERY Trigger
To make the most of the PRE-QUERY Trigger, follow these best practices:
- Keep conditions relevant
- Do not overload the query with too many restrictions unless necessary.
- Avoid hardcoding values
- Use global variables, parameters, or user inputs for flexibility.
- Use DEFAULT_WHERE carefully
- Reset or clear it after usage if it should not persist across queries.
- Test performance impact
- Ensure additional WHERE clauses do not slow down queries unnecessarily.
- Maintain security checks
- Use PRE-QUERY to enforce user-specific restrictions that cannot be bypassed.
Common Mistakes to Avoid
While PRE-QUERY is powerful, developers often make mistakes that lead to unexpected results:
- Placing validation logic inside PRE-QUERY: Validation should occur in
WHEN-VALIDATE-ITEMor similar triggers, not here. - Overwriting user input: Ensure that automatic conditions do not erase what the user has already entered.
- Not resetting DEFAULT_WHERE: Leaving additional WHERE clauses active can cause confusion or missing records in future queries.
- Using it for post-processing: PRE-QUERY is not meant for formatting or calculations on fetched data; use POST-QUERY for that.
Benefits of Using PRE-QUERY Trigger
When used properly, the PRE-QUERY Trigger offers several benefits:
- Improved security: Restricts unauthorized access to data.
- User convenience: Automatically applies filters, saving time.
- Consistency: Ensures that certain conditions are always applied.
- Performance optimization: Reduces data load by fetching only what is necessary.
Conclusion
The PRE-QUERY Trigger in Oracle Forms is a vital tool for controlling data retrieval. By firing just before a query runs, it gives developers the power to add conditions, enforce security, and ensure users only see relevant data. When used alongside POST-QUERY, it helps create a well-structured data handling flow in Oracle Forms applications.
By following best practices and avoiding common mistakes, you can use the PRE-QUERY Trigger to make your forms more secure, efficient, and user-friendly. Whether you want to apply default filters, restrict access by department, or optimize performance, this trigger is one of the most effective ways to manage queries in Oracle Forms.

