Search code examples
oracle-apexoracle19c

How to use page item value inside a trigger query in oracle?


I want the following trigger to be run correctly but it rise an error which is: bad bind variable 'P23_ID'. The trigger query is:

Create or replace trigger "newTRG"
Before
Insert on "my_table"
For each row
Begin
If :new."ID" is null then
Insert into my_table (ID) values (:P23_ID);
end if;
End;

Solution

  • Use the v() syntax:

    create or replace trigger "newTRG" before
       insert on "my_table"
       for each row
    begin
       if :new."ID" is null then
          insert into my_table ( id ) values (v('P23_ID'));
    
       end if;
    end;
    

    On a side note, if this is a primary key value it is a lot easier to use identity columns (the new way) or a sequence (the old way) to populate your column. Doing this from a page item is error prone.