Search This Blog

Tuesday, June 24, 2025

How Do I Add a Row Using SQL in Oracle APEX

 Adding a row to a database table is a common requirement when working with Oracle APEX applications. This can be achieved using SQL INSERT statements in different ways, such as through a SQL command, PL/SQL process, Dynamic Actions, or Form Submissions. This tutorial covers multiple approaches to inserting a row using SQL in APEX.

Using SQL to Add a Row in APEX

1. Using SQL Commands to Insert Data

If you have direct access to the SQL Commands interface in Oracle APEX, you can use a simple INSERT statement to add a row to a table.

Example: Inserting Data into an Employees Table

INSERT INTO employees (emp_id, emp_name, department, salary)  

VALUES (101, 'John Doe', 'IT', 5000);

  • This command adds a new employee with ID 101, name 'John Doe', department 'IT', and salary 5000.

  • Run this query in SQL Commands or any SQL Worksheet.

2. Using a PL/SQL Process in APEX to Insert Data

When using an APEX form, you can create a PL/SQL process to insert data when a user submits the form.

Steps to Insert a Row Using a Form

  1. Create a New Form

    • In APEX, create a Form page based on your table (e.g., employees).

    • Add page items (P1_EMP_ID, P1_EMP_NAME, P1_DEPARTMENT, P1_SALARY).

    • Add a Submit Button.

  2. Add a PL/SQL Process

    • Navigate to Processing > Create a New Process.

    • Select PL/SQL Process and enter the following code:

BEGIN  

    INSERT INTO employees (emp_id, emp_name, department, salary)  

    VALUES (:P1_EMP_ID, :P1_EMP_NAME, :P1_DEPARTMENT, :P1_SALARY);

    

    COMMIT;  

END;

  • This process executes when the user clicks Submit, inserting a new row.

  • Bind variables (:P1_EMP_ID, etc.) retrieve values from form fields.

3. Inserting a Row Using a Dynamic Action

You can insert a row dynamically using Dynamic Actions when a button is clicked.

Steps to Set Up Dynamic Action for Insert

  1. Create a Button (e.g., Insert Data)

  2. Go to Dynamic Actions and create a new True Action 

    • Action: Execute PL/SQL Code

    • PL/SQL Code:

BEGIN  

    INSERT INTO employees (emp_id, emp_name, department, salary)  

    VALUES (:P1_EMP_ID, :P1_EMP_NAME, :P1_DEPARTMENT, :P1_SALARY);

    

    COMMIT;  

END;

  1. Set Items to Submit 

    • Select P1_EMP_ID, P1_EMP_NAME, P1_DEPARTMENT, P1_SALARY

    • These values will be passed into the query.

When the button is clicked, the new employee record is inserted without submitting the page.

4. Inserting Data Using a RESTful API Call

APEX allows inserting a row via REST services, useful when integrating with external applications.

Example REST API for Inserting Data

POST /ords/hr/employees/

{

  "emp_id": 102,

  "emp_name": "Jane Smith",

  "department": "HR",

  "salary": 5500

}

This method enables data insertion via API calls from external systems.

5. Inserting Data Using a Custom PL/SQL Procedure

If inserting rows is a frequent operation, use a PL/SQL procedure for reusability.

Creating a Stored Procedure for Insertion

CREATE OR REPLACE PROCEDURE add_employee (

    p_emp_id IN NUMBER,  

    p_emp_name IN VARCHAR2,  

    p_department IN VARCHAR2,  

    p_salary IN NUMBER  

) AS  

BEGIN  

    INSERT INTO employees (emp_id, emp_name, department, salary)  

    VALUES (p_emp_id, p_emp_name, p_department, p_salary);  

    

    COMMIT;  

END add_employee;

/

Calling the Procedure in APEX

BEGIN  

    add_employee(:P1_EMP_ID, :P1_EMP_NAME, :P1_DEPARTMENT, :P1_SALARY);

END;

This allows inserting data using a PL/SQL function call.


Best Practices for Adding Rows in APEX

  • Use Bind Variables to prevent SQL injection (:P1_EMP_ID).

  • Enable Error Handling to handle duplicate keys or missing values.

  • Use Commit Statements to ensure data persistence.

  • Validate Input Fields to avoid incorrect or missing data.


Adding a row using SQL in Oracle APEX can be done in multiple ways, depending on the use case. Whether using SQL Commands, PL/SQL processes, Dynamic Actions, RESTful APIs, or Stored Procedures, APEX provides flexibility for inserting data efficiently.

How Do I Use a Stored Procedure in an APEX Report

 Introduction

Using a stored procedure in an Oracle APEX report allows you to centralize complex data retrieval logic within the database, improving maintainability and performance. Instead of embedding lengthy SQL queries directly in your report region, you can write a stored procedure that returns a result set via a ref cursor. This approach provides flexibility to reuse the procedure across multiple reports and applications while keeping your APEX pages cleaner and more efficient.

1. Using a Stored Procedure in a SQL Query

If a stored procedure returns a single value, it cannot be directly used in an Interactive Report or Classic Report. However, if it returns a table, it can be queried as follows:

SELECT emp_id, emp_name, salary FROM employees 

WHERE salary > (SELECT salary FROM employees WHERE emp_id = 101);

If the stored procedure returns a table, use:

SELECT * FROM TABLE(my_stored_procedure(:P1_EMP_ID));


2. Using a Stored Procedure in a RESTful Web Service

Oracle APEX allows using stored procedures in RESTful Web Services to return JSON responses.

  1. Create a RESTful Web Service in APEX

  2. Set the SQL Query as:

BEGIN 

    get_employee_salary(:P1_EMP_ID, :P1_SALARY);

END;

Return JSON Output:

{

    "EMP_ID": 101,

    "SALARY": 6000

}


Updating Data with a Stored Procedure

This procedure updates an employee's salary.

CREATE OR REPLACE PROCEDURE update_employee_salary (

    p_emp_id IN NUMBER, 

    p_new_salary IN NUMBER

) AS 

BEGIN 

    UPDATE employees SET salary = p_new_salary WHERE emp_id = p_emp_id;

    COMMIT;

END update_employee_salary;

/

Calling the Procedure from APEX

  1. Create an APEX Form with: 

    • P1_EMP_ID

    • P1_NEW_SALARY

  2. Create a PL/SQL Process:

BEGIN 

    update_employee_salary(:P1_EMP_ID, :P1_NEW_SALARY);

END;

  1. Submit the form to update the salary.

Best Practices for Using Stored Procedures in APEX

  • Use Exception Handling – Prevent runtime errors.

  • Optimize Queries – Ensure queries inside procedures use indexes for better performance.

  • Avoid Hardcoded Values – Use dynamic parameters instead.

Example with Exception Handling

CREATE OR REPLACE PROCEDURE safe_add_employee (

    p_emp_id IN NUMBER, 

    p_emp_name IN VARCHAR2, 

    p_salary IN NUMBER

) AS 

BEGIN 

    INSERT INTO employees (emp_id, emp_name, salary) 

    VALUES (p_emp_id, p_emp_name, p_salary);


    COMMIT;

EXCEPTION

    WHEN DUP_VAL_ON_INDEX THEN

        RAISE_APPLICATION_ERROR(-20001, 'Employee ID already exists.');

    WHEN OTHERS THEN

        RAISE_APPLICATION_ERROR(-20002, 'An unexpected error occurred.');

END safe_add_employee;

/

This ensures the procedure does not fail if an employee ID already exists.


Stored procedures in Oracle APEX allow for modular, reusable, and efficient PL/SQL code execution. They improve performance and maintainability by separating logic from the application interface.

By integrating stored procedures in forms, reports, dynamic actions, and RESTful APIs, you can build robust and scalable APEX applications.

Conclusion
Incorporating stored procedures into your APEX reports enhances modularity and simplifies application maintenance by separating data logic from presentation. By returning result sets through ref cursors, stored procedures offer a powerful way to manage complex queries and filtering within the database. Leveraging this method helps build scalable, reusable, and high-performing reports in your Oracle APEX applications.

How Do I Execute the Stored Procedure in APEX

 Introduction

Executing a stored procedure in Oracle APEX allows you to leverage powerful database logic directly from your web application. Stored procedures can perform tasks like inserting, updating, or deleting data, as well as complex business operations. By calling these procedures within APEX page processes, buttons, or dynamic actions, you can keep your application code clean and maintainable while centralizing key logic in the database.

1. Calling a Stored Procedure from SQL Commands

After creating the procedure, you can call it from SQL Commands in Oracle APEX.

BEGIN 

    add_employee(101, 'Alice Johnson', 6000);

END;

/

This inserts an employee with ID 101, name Alice Johnson, and salary 6000.


2. Calling a Stored Procedure in a PL/SQL Process in APEX

To use the stored procedure in an APEX form:

  1. Create a form with three text fields: 

    • P1_EMP_ID

    • P1_EMP_NAME

    • P1_SALARY

  2. Add a Submit Button

  3. Create a PL/SQL Process that Calls the Procedure

BEGIN 

    add_employee(:P1_EMP_ID, :P1_EMP_NAME, :P1_SALARY);

END;

When the user enters values in the form and clicks Submit, the procedure inserts the data into the table.


Stored Procedure with an OUT Parameter

This procedure retrieves the salary of an employee based on the employee ID.

CREATE OR REPLACE PROCEDURE get_employee_salary (

    p_emp_id IN NUMBER, 

    p_salary OUT NUMBER

) AS 

BEGIN 

    SELECT salary INTO p_salary FROM employees WHERE emp_id = p_emp_id;

END get_employee_salary;

/

Calling the Procedure in PL/SQL

DECLARE

    v_salary NUMBER;

BEGIN 

    get_employee_salary(101, v_salary);

    DBMS_OUTPUT.PUT_LINE('Employee Salary: ' || v_salary);

END;

/

This fetches the salary of employee 101 and displays it using DBMS_OUTPUT.PUT_LINE.

Conclusion
Calling stored procedures in Oracle APEX simplifies application development by separating business logic from the user interface. It enables reuse of database operations and improves performance by reducing redundant code. With simple PL/SQL blocks or processes, you can execute stored procedures efficiently, making your applications more scalable, secure, and easier to maintain.

UI Defaults

 In Oracle APEX, User Interface (UI) Defaults are a set of metadata-driven, table- and column-scoped attributes that APEX consults when it g...