Handling BOOLEAN Parameters in PL/SQL Functions for Oracle SQL SELECT Statements

Nov 22, 2025 · Programming · 12 views · 7.8

Keywords: Oracle Database | PL/SQL Functions | BOOLEAN Parameters | Wrapper Functions | Data Type Conversion | SQL Invocation

Abstract: This technical paper addresses the ORA-00904 error encountered when invoking PL/SQL functions with BOOLEAN parameters within Oracle SQL SELECT statements. By analyzing Oracle's data type limitations, it presents an effective wrapper function solution that converts BOOLEAN parameters to SQL-compatible types, detailing implementation steps and best practices. The paper also compares alternative approaches, providing developers with practical technical guidance.

Problem Background and Challenges

In Oracle database development, developers frequently need to invoke PL/SQL functions within SQL statements. However, when these functions contain BOOLEAN type parameters, direct invocation encounters technical limitations. Specifically, attempting to use TRUE or FALSE literals in SELECT statements results in the ORA-00904: "TRUE": invalid identifier error.

Analysis of Oracle Data Type Limitations

According to Oracle official documentation, the BOOLEAN data type is specific to PL/SQL and cannot be used directly in SQL contexts. This means:

Wrapper Function Solution

To address these limitations, the most effective solution is to create a wrapper function. The core concept of this approach is:

function get_something_wrapper(name in varchar2,
                           ignore_notfound in varchar2) return varchar2
is
begin
    return get_something(name, (upper(ignore_notfound) = 'TRUE'));
end;

This wrapper function operates as follows:

  1. Accepts parameters of VARCHAR2 type, which is fully supported by SQL
  2. Converts string parameters to BOOLEAN values within the function
  3. Invokes the original function and returns the result

Implementation Details and Invocation Methods

The implementation of the wrapper function should consider several key aspects:

-- Correct way to call the wrapper function
select get_something_wrapper('NAME', 'TRUE') from dual;

In the conversion logic, we use upper(ignore_notfound) = 'TRUE' to determine the Boolean value. This design assumes:

Comparison of Alternative Approaches

Besides the wrapper function method, other viable alternatives exist:

Numeric Representation

-- Using 1 for TRUE and 0 for FALSE
SELECT CASE WHEN (10 > 0) THEN 1 ELSE 0 END AS MY_BOOLEAN_COLUMN
  FROM DUAL

String Representation

-- Using strings to directly represent Boolean values
SELECT CASE WHEN (10 > 0) THEN 'true' ELSE 'false' END AS MY_BOOLEAN_COLUMN
 FROM DUAL

These methods have their respective advantages and disadvantages: numeric representation is suitable for integration with ORM frameworks like Hibernate and MyBatis, while string representation is more human-readable.

Best Practice Recommendations

Based on practical development experience, we recommend:

  1. Consistency Principle: Use the same Boolean representation method throughout the project
  2. Error Handling: Include parameter validation in wrapper functions to ensure input value validity
  3. Performance Considerations: For frequently called functions, consider using numeric comparisons instead of strings
  4. Documentation Maintenance: Clearly document the usage and parameter conventions of wrapper functions

Technical Extensions and Future Outlook

As Oracle database versions evolve, data type support may change. Developers should:

By adopting the wrapper function approach, developers can effectively bypass Oracle's data type limitations, enabling smooth usage of PL/SQL functions with BOOLEAN parameters in SQL environments, thereby enhancing code usability and maintainability.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.