ORA-24344:编译错误成功 - 触发APEX

时间:2016-05-23 13:48:42

标签: sql database oracle plsql oracle-apex

我一直在解决此触发器,当我运行脚本时,它会告诉我之前的错误消息。我似乎无法弄清楚为什么它不能正确编译,每个pl / sql触发器教程似乎都有我的触发器具有的结构。代码如下:

create
    or replace trigger new_artist before insert
        on
        Artist referencing new as nvartist declare counter number;

begin select
    count( * ) into
        counter
    from
        Performer
    where
        Stage_name = nvartist.Stage_name;

if counter > 0 then signal sqlstate '45000';
else insert
    into
        Artist
    values(
        nvartist.Stage_name,
        nvartist.Name
    );

insert
    into
        Performer
    values(nvartist.Stage_name);
end if;
end;

它检查新艺术家是否已经存在于其超类型(Performer)中,如果它确实存在,如果它没有将它们插入艺术家(Stage_name varchar2,Name varchar2)和Performer(Stage_name),则会给出错误。 Performer(和Artist的兄弟)的另一个子类型是Band(Stage_name),它又与Artist有关系。为什么编译器会因为这个触发器而对我大喊大叫?

提前致谢

1 个答案:

答案 0 :(得分:0)

您可能想尝试这种变体(我略微修改了表格的名称)。 使用示例数据创建表:

CREATE table test_artist(
   stage_name varchar2(100)
 , name varchar2(100)
);

create table test_performer(
   stage_name varchar2(100)
);

/*inserting test performer on which trigger will rise an error*/
insert into test_performer
select 'performer_1' from dual;

创建触发器:

create or replace trigger new_artist
  before insert
  on TEST_ARTIST 
  referencing new as nvartist
  for each row

declare
  counter number;
begin
  select count(*)
  into counter
  from test_performer
  where Stage_name = :nvartist.Stage_name;

  if counter > 0 then
      --signal sqlstate '45000' ;
      raise_application_error( -20001, 'No insertion with existing Performer');
  else
      /*you cant update the same table, in other case you'll get 
      ora-04091 table mutating error.      
      But nevertheless this values will be inserted by sql triggered this trigger.*/
      --insert into test_artist values(:nvartist.Stage_name, :nvartist.Name);      
      insert into test_performer values(:nvartist.Stage_name);
  end if;
end new_artist;

之后,这个插入将起作用,因为没有' performer_2'在' test_performer'表:

insert into test_artist
select 'performer_2', 'name_2' from dual;

这将失败:

insert into test_artist
select 'performer_1', 'name_1' from dual;