Oracle APEX Database Trigger - Problems with referencing database columns

家住魔仙堡 提交于 2019-12-11 08:58:00

问题


I have a colon delimited list of values being stored in a varchar2 column, ORDER_PARTS_LIST, of my Oracle database.

(I understand that storing data in a list like this might not be best practice but for now just ignore that fact.)

Here are the relevant table columns:

 ORDER_TABLE(
    ORDER_NUMBER number,
    ORDER_PARTS_LIST varchar(4000))

 PARTS_TABLE(
    PART_NUMBER varchar(20),
    ASSIGNED_ORDER_NUMBER number)

I have a conditional trigger:

 create or replace trigger "ORDER_PARTS_T1"
 BEFORE
 insert or update or delete on "ORDER_TABLE"
 for each row
 begin
   if :new.ORDER_PARTS_LIST LIKE '%'+PART_NUMBER+'%' then
     update PARTS_TABLE set ASSIGNED_ORDER_NUMBER = :ORDER_NUMBER;   
   end if;

 end;

When I run this trigger I get the following error:

 PLS-00201: identifier 'PART_NUMBER' must be declared

What is supposed to happen is that the trigger checks which PART_NUMBERs, in PARTS_TABLE, are included in the ORDER_PARTS_LIST, in the ORDER_TABLE, and then inserts the ORDER_NUMBER, for the affected row in ORDER_TABLE, into the ASSIGNED_ORDER_NUMBER column, of PARTS_TABLE.

In the end, all the PARTS in an ORDER should be flagged with that ORDER's NUMBER.

Does that make ANY sense???

I am not certain exactly how to properly define the variables in this trigger so that it runs and honestly I have a few doubts as to whether or not the trigger would do what I think it should even if those worked. ANY suggestions or help in getting the trigger functioing like I have defined it should would be great. Thanks in advance.


回答1:


You can do string matching to test each row:

create or replace trigger "ORDER_PARTS_T1"
BEFORE
insert or update on "ORDER_TABLE"
for each row
begin
  update PARTS_TABLE p
  set p.ASSIGNED_ORDER_NUMBER = :new.ORDER_NUMBER
  where instr(':' || :new.ORDER_PARTS_LIST || ':'
             ,':' || p.PART_NUMBER || ':') > 0;
end;

So for example, if ORDER_PARTS_LIST is '123:456:789', the INSTR will find matches for the ids 123, 456 and 789, but not 124, 45 or 8, for example.

When parts are removed from an order you will need a different trigger to NULL the appropriate fields in PARTS_TABLE:

create or replace trigger "ORDER_PARTS_T1"
BEFORE
update on "ORDER_TABLE"
for each row
begin
  update PARTS_TABLE p
  set p.ASSIGNED_ORDER_NUMBER = NULL
  where instr(':' || :new.ORDER_PARTS_LIST || ':'
             ,':' || p.PART_NUMBER || ':') = 0
  and instr(':' || :old.ORDER_PARTS_LIST || ':'
             ,':' || p.PART_NUMBER || ':') > 0;
end;



回答2:


You are creating a trigger on the ORDER_TABLE. Since the ORDER_TABLE does not contain a column named PART_NUMBER, Oracle is unable to find the identifier 'PART_NUMBER' as it belongs to the PARTS_TABLE.

You will need to write a separate query in your trigger to access the PART_NUMBER in PARTS_TABLE.




回答3:


Not entirely sure how this all fits together (seems like this won't account for the same part on multiple orders), but it looks like what you're trying to do is something like this:

 create or replace trigger "ORDER_PARTS_T1"
 BEFORE
 insert or update or delete on "ORDER_TABLE"
 for each row
 begin
    update parts_table
    set assigned_order_number = :new.ORDER_NUMBER
    where part_number in (:new.order_parts_list);
 end;


来源:https://stackoverflow.com/questions/9024336/oracle-apex-database-trigger-problems-with-referencing-database-columns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!