在oracle中插入时更新其他列的值

时间:2016-06-30 12:30:52

标签: sql oracle11g oracle-sqldeveloper

我的任务非常简单,假设我的表有3列,即temp(ids,name_c,UID_c),我有前两列值,第三列可以为空。我想要做的是每当插入这两个值时,必须使用新值更新第三列的值(插入后)。即两个值的连接。

对于Ex。

 insert into temp(ids, name_c) values(did.nextval,'Andrew');

结果应为

     1         Andrew           Andrew_1

所以我正在为此目的使用触发器

  create or replace trigger triggerDemo
  after INSERT
  on temp
  for each row
  declare  
  /*    pragma autonomous_transaction;*/
     user_name varchar2(50);
     current_val number;
  begin    
    select did.currval into current_val from dual; /* did is sequence */
    select names into user_name from temp where ids = current_val;
    update temp set uid_c = user_name||'_'||ids where ids = current_val;
    end;

当我插入值时,我收到此错误

  01403. 00000 -  "no data found"
  *Cause:    No data was found from the objects.
  *Action:   There was no data from the objects which may be due to end of fetch.

1 个答案:

答案 0 :(得分:0)

首先,您需要before insert触发器,而不是after insert触发器。其次,决定是否要在输入或触发器中计算id。你可以这样做:

create or replace trigger triggerDemo before INSERT on temp
for each row
  /*    pragma autonomous_transaction;*/
begin  
    if :new.current_val is null then  
        select did.currval into :new.ids from dual; /* did is sequence */
    end;
    select :new.user_name || '_' || :new.ids into :new.uid_c from dual;
end;