+ -
当前位置:首页 → 问答吧 → 触发器未找到数据问题

触发器未找到数据问题

时间:2011-10-12

来源:互联网

刚学数据库,在学习触发器时出现如下问题:
SQL CODE:
create or replace trigger insert_ll
before insert on ll
--after inert on ll
referencing new as new old as old
for each row
declare
d_a char(10);
d_b number(10);
PRAGMA AUTONOMOUS_TRANSACTION;
begin
  select ll.a,ll.b into d_a,d_b from ll where ll.a = :new.a;
  if inserting then
  insert into ll1(a,b)values(d_a,d_b);
  end if;
  commit;
  end insert_ll;

在执行数据插入前 ll表有1条数据
a b
1 100

当我执行insert into ll values('2',200);语句时报如下错误:
ora-01403:未找到数据
ora-06512:在system.insert_ll,line 6
ora-04088:触发器system.insert_ll执行过程中出错
请问各位这个问题怎么解决啊。


作者: lilin0691   发布时间: 2011-10-12

提示的错误很明确,ora-01403:未找到数据
因为在执行select ll.a,ll.b into d_a,d_b from ll where ll.a = :new.a;的时候出现错误。
insert into ll values('2',200); 此时 new.a=2 
你ll表中没有 a=2的记录,所以报错。
你执行insert into ll values('1',200); 就没错了。
或者加个异常处理。如
SQL code
create or replace trigger insert_ll
before insert on ll
referencing new as new old as old
for each row
declare
  d_a char(10);
  d_b number(10);
  PRAGMA AUTONOMOUS_TRANSACTION;
begin
  select ll.a, ll.b into d_a, d_b from ll where ll.a = :new.a;
  if inserting then
    insert into lll (a, b) values (d_a, d_b);
  end if;
  commit;
exception
  when others then
    return;
end insert_ll;

作者: jdsnhan   发布时间: 2011-10-13

你的逻辑有问题,你想把新插入的值插入到另一张表里去
实际很简单
直接
insert into lll(a, b) values(:new.a, :new.b);

作者: opps_zhou   发布时间: 2011-10-13

SQL code
同一楼的写法.....!!

作者: cosio   发布时间: 2011-10-13

热门下载

更多