【问题标题】:Updating the value of other column while insertion in oracle在 oracle 中插入时更新其他列的值
【发布时间】:2016-06-30 12:30:52
【问题描述】:

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

例如。

 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.

【问题讨论】:

    标签: sql oracle11g oracle-sqldeveloper


    【解决方案1】:

    首先,您需要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;
    

    【讨论】:

      猜你喜欢
      • 2019-01-20
      • 1970-01-01
      • 2011-04-19
      • 2018-01-19
      • 1970-01-01
      • 1970-01-01
      • 2014-07-11
      • 1970-01-01
      • 2021-05-21
      相关资源
      最近更新 更多