Introduction
In Oracle APEX, providing users with a simple way to add data is essential for building efficient and user-friendly applications. One common requirement is to allow users to add a new row to a table by clicking a button. This action can be tied to a form or dynamic process, enabling seamless data entry without navigating away from the current page. In this guide, you'll learn how to configure a button in APEX to insert a new row into a table using PL/SQL logic and built-in components.
To add a row to a table with a button in Oracle APEX, you can create a form page or use a custom button and dynamic action that runs PL/SQL code. The most common use case is inserting data into a table when the user clicks a “Submit” or “Add Row” button. Below is a detailed walkthrough of how to do this using a manual insert with a button and PL/SQL process.Step 1: Prepare Your Table
Ensure that you have a table ready for inserting rows. For example:
CREATE TABLE TASKS (
TASK_ID NUMBER GENERATED BY DEFAULT ON NULL AS IDENTITY,
TASK_NAME VARCHAR2(255),
ASSIGNED_TO VARCHAR2(100),
STATUS VARCHAR2(50),
CREATED_DATE DATE DEFAULT SYSDATE,
CONSTRAINT TASKS_PK PRIMARY KEY (TASK_ID)
);
Step 2: Create a Page with Form Items and Button
-
In the APEX App Builder, create a new Blank Page.
-
In Page Designer, add three page items:
-
P1_TASK_NAME
(Text Field) -
P1_ASSIGNED_TO
(Text Field) -
P1_STATUS
(Select List or Text Field)
-
-
Add a button to the page:
-
Label:
Add Row
-
Static ID (optional but helpful):
ADD_ROW_BTN
Position it in the Button Bar or directly under the items.
-
Step 3: Create a PL/SQL Process to Insert the Row
-
In Page Designer, go to the Processing section.
-
Click the ➕ icon to add a new Process.
-
Name it
Insert Task Row
. -
Set Type to
PL/SQL Code
. -
Enter the following PL/SQL:
INSERT INTO TASKS (TASK_NAME, ASSIGNED_TO, STATUS)
VALUES (:P1_TASK_NAME, :P1_ASSIGNED_TO, :P1_STATUS);
-
Set Server-side Condition:
-
When Button Pressed =
ADD_ROW_BTN
(or whatever button you created)
-
Set Process Point =
After Submit
.
Step 4: Optional – Clear Form Items After Insert
If you want to clear the inputs after the row is inserted:
-
Create a new Process.
-
Name it
Clear Form
. -
Type:
PL/SQL Code
. -
Enter:
:P1_TASK_NAME := NULL;
:P1_ASSIGNED_TO := NULL;
:P1_STATUS := NULL;
-
Set it to run After Submit, after the insert process.
Set the same button condition.
Step 5: Run the Page and Test
-
Run the application.
-
Enter values into the fields and click the Add Row button.
-
The new data should now be inserted into the
TASKS
table. You can verify by creating a Classic Report on
TASKS
or checking via SQL Workshop.
Optional Enhancements
-
Add Validations: Ensure required fields are filled out before insert.
-
Display Success Message: Go to Processing > Messages and define a success message.
Use Dynamic Actions: For more control, you can create a dynamic action that submits the page and runs the PL/SQL process when the button is clicked.
In Oracle APEX, adding a row to a table using a button is easy to implement using form elements, a submit button, and a PL/SQL process. This approach works well for custom data-entry pages or lightweight admin tools. By combining user input with backend logic, you can give your users an efficient way to add records while maintaining full control over validations, process flow, and user experience.
In this example we are going to use two fields and one table
Fields: P10_NEW_1 and P10_NEW_2
Table:
Using Table1
Has 3 columns
ID
VALUE1 – varchar2
VALUE2- varchar2
Has 1 button: named “add”
Step 1 -Create a page with both fields and a button
Step 2- Add a dynamic Action to the button
Step 3- In the Dynamic Action TRUE section
In Identification choose Action: Execute Server-side Code
In the Settings
Language (PL/SQL)
PL/SQL Code: Add the following code
INSERT INTO TABLE1 (value1,value2)
VALUES(:P10_NEW_1,:P10_NEW_2);
In the Items to Submit enter all of the fields that you want to pass to the database. If you don’t do this then the values are not entered into the database.
Conclusion
Adding a row to a table with a button in Oracle APEX is a flexible and powerful technique that enhances user interactivity. Whether using a form-based approach or a dynamic action tied to a PL/SQL process, you can tailor the behavior to meet your application's needs. Once set up, this functionality streamlines data entry, minimizes user effort, and helps maintain clean, consistent records throughout your application.
No comments:
Post a Comment