Sunday, February 26, 2012

Oracle Forms- Insert an auto-increment( next val)

Here are two ways to create an auto-increment on Oracle forms 10g.
1. Create a sequence number,

CREATE SEQUENCE s_customer_id
MINVALUE 1
MAXVALUE 9999999
INCREMENT BY 1
START WITH 216
NOCACHE
NOORDER
NOCYCLE

On the object navigator click on the item and right click on the item the select smart trigger, then when-new-item-instance, then type the following on the pl/sql editor,

DECLARE
new_account_id accounts.account_id%type;
begin
select s_account_id.nextval into new_account_id from dual;
:account_id:=new_account_id;
end;
Then run the form, on every new instance, a new is inserted.
2. Using A PRE-INSERT- a trigger that fires once before each new record is inserted in a commit process

Create a Trigger
In the Object Navigator, highlight the Trigger item under the form data block and click on the green ‘+’ sign to create a trigger.

PRE-INSERT trigger
In the Trigger window, type ‘P’ then ‘R,’ and then select the ‘PRE-INSERT’ trigger.
PL/SQL Editor
In the PL/SQL Editor, write a select statement to assign a new sequence number to account_id
(PL/SQL Editor)
SELECT s_account_id.nextval INTO :accounts.account_id
FROM dual;
Compile a trigger
Compile the trigger and then close the window.
Run the Form
Run the application.

In case of any query, don't hesitate to ask.

No comments:

Post a Comment