Search This Blog

Showing posts with label sql biding page. Show all posts
Showing posts with label sql biding page. Show all posts

Friday, July 18, 2025

Binding a Page Item to a SQL Call as a Variable in Oracle APEX

 In Oracle APEX, page items play a crucial role in capturing user input and interacting with SQL queries or PL/SQL processes. One of the most powerful features of APEX is the ability to bind these page items to SQL statements and PL/SQL blocks as variables. This enables dynamic filtering, flexible logic execution, and personalized content based on user interactions. Understanding how to properly bind page items to SQL calls is essential for building responsive and data-driven applications.

Using Page Items as Bind Variables in SQL

Binding a page item to a SQL statement allows the item’s value to influence the results returned by a query or executed in logic. Oracle APEX supports bind variable syntax using the colon (:) prefix for use inside SQL and PL/SQL code.

Syntax

SELECT * FROM employees WHERE department_id = :P1_DEPT_ID

In this example, :P1_DEPT_ID is a page item whose value will be used during the query execution. At runtime, APEX will substitute the value of that item when executing the SQL.

How to Bind Page Items in SQL Queries

  1. Create a page item:

    • Go to your page in Page Designer.

    • Add a new item, for example: P1_EMP_ID.

  2. Reference the item in your SQL query:

    • In a region (like Classic Report or Interactive Report), use the item in a WHERE clause:

      SELECT * FROM employees WHERE employee_id = :P1_EMP_ID
      
  3. Use Dynamic Actions or processes to set the item’s value before the SQL query runs, if needed.

Using Page Items in PL/SQL Processes

In PL/SQL, you can access the value of a page item using the same bind variable syntax:

DECLARE
  l_emp_id NUMBER;
BEGIN
  l_emp_id := :P1_EMP_ID;

  UPDATE employees
  SET salary = salary * 1.1
  WHERE employee_id = l_emp_id;
END;

This approach is useful for processes like validations, computations, or custom buttons.

Detailed Example: Filter Report Based on Department

  1. Create a select list item: P1_DEPT_ID

    • List of values query:

      SELECT department_name d, department_id r FROM departments
      
  2. Create a Classic Report region:

    • SQL Query:

      SELECT * FROM employees
      WHERE department_id = :P1_DEPT_ID
      
  3. Set the report to refresh using a Dynamic Action when the select list changes.

Best Practices

  • Always validate user input before using it in dynamic SQL to prevent SQL injection.

  • Use :ITEM_NAME syntax only in SQL and PL/SQL that are processed by the APEX engine.

  • For stored procedures outside APEX, use APEX API functions to retrieve item values explicitly.

  • Avoid using substitution syntax (&ITEM.) inside SQL queries—it is suitable only for static text and can pose security risks.

  • Use page items with clear naming conventions (P1_, P2_) to avoid confusion across multiple pages.

  • Used in SQL defined Regions.

  • The name of the item must correspond to the variable name.

  • Not case sensitive.

  • Limited to 30 characters in length.

  • The data type is always varchar2.

Example:

SELECT * FROM employees WHERE last_name like '%' || :SEARCH_STRING || '%'

Since the variable can only be a varchar2, you might need to convert it explicitly in order to make it work.

Example:

SELECT * FROM employees WHERE start_date < to_date(:DATE_STRING,'DD-MON-YYYY')


Oracle APEX Documentation Link

Refer to the official documentation on bind variables and SQL support here:
https://docs.oracle.com/en/database/oracle/apex/24.1/htmdb/using-bind-variables.html

Conclusion

Binding a page item to a SQL call as a variable is a foundational technique in Oracle APEX development. It enables the creation of interactive, responsive applications that respond to user input in real time. By leveraging the power of bind variables, you ensure your applications are not only dynamic but also secure and efficient. Mastering this concept will allow you to connect user interface elements directly to the database logic, providing a seamless experience for both developers and users.

How Do I Make a Faceted Search Map Page in Oracle APEX

Combining faceted search with a map region in Oracle APEX enables users to filter data visually and spatially at the same time. This design ...