ORA-01732:具有触发器和联合视图的此视图上的数据操作操作不合法

时间:2016-12-30 23:44:09

标签: oracle triggers union

您好我创建了两个表

 CREATE TABLE "LCM_001"."LCM_BDT$ACT" 
   (    "ID" NUMBER(*,0) NOT NULL ENABLE, 
    "SHORTDESCRIPTION" VARCHAR2(25 CHAR), 
    "LONGDESCRIPTION" VARCHAR2(500 BYTE), 
    "VALIDFROM" TIMESTAMP (6) WITH LOCAL TIME ZONE, 
    "VALIDTO" TIMESTAMP (6) WITH LOCAL TIME ZONE, 
    "VERSION" NUMBER(*,0) NOT NULL ENABLE
   ) 

第二个表与第一个表完全相同,所以我用“

创建了它
create table LCM_BDT$HIST as select * from LCM_BDT$ACT where 1=0;

然后我创建了一个视图

select * from LCM_BDT$ACT 
Union
select * from LCM_BDT$HIST 

然后我写了一个而不是触发器

create or replace TRIGGER LCM_BDT_DML
  instead of insert 
  on LCM_BDT
  -- for each row
declare
    vAct LCM_BDT$ACT%rowtype;
    vHist LCM_BDT$HIST%rowtype; 
  begin
    IF INSERTING THEN
      select BDT_SEQ.nextval into vAct.id from dual;
      vAct.SHORTDESCRIPTION := :new.SHORTDESCRIPTION;
      vAct.LONGDESCRIPTION := :new.LONGDESCRIPTION;
      vAct.VALIDFROM := sysdate;
      vAct.VALIDTO := TO_TIMESTAMP('31.12.3999','DD.MM.YYYY');
      vAct.Version := 1;
      Insert into LCM_BDT$ACT values vAct;
    END IF;

    IF UPDATING THEN
--      vHist.ID  := :old.ID;
--      vHist.SHORTDESCRIPTION := :old.SHORTDESCRIPTION;
--      vHist.LONGDESCRIPTION := :old.LONGDESCRIPTION;
--      vHist.VALIDFROM := :old.VALIDFROM;
--      vHist.VALIDTO := sysdate;
--      vHist.VERSION := :old.Version;
--      
--      Insert into LCM_BDT$ACT values vAct;
--    -- new record
--      UPDATE LCM_BDT$HIST set 
--      vAct.SHORTDESCRIPTION := :new.SHORTDESCRIPTION;
--      vAct.LONGDESCRIPTION := :new.LONGDESCRIPTION;
--      vAct.VALIDFROM := sysdate;
--      vAct.VALIDTO := TO_TIMESTAMP('31.12.3999','DD.MM.YYYY');
--      vAct.Version := :old.Version +1;
--      Insert into LCM_BDT$HIST values vHist;

    delete from LCM_BDT where id = :old.ID;
    END IF;
END LCM_BDT_DML;

问题是我总是遇到SQL错误:

SQL-Fehler:ORA-01732:此视图上的数据操作操作不合法 01732. 00000 - “数据操作操作在此视图中不合法” *原因:
*操作:

有什么建议吗?

谢谢

1 个答案:

答案 0 :(得分:2)

您的if updating分支包括:

delete from LCM_BDT where id = :old.ID;

这是尝试从同一视图中删除此触发器,而不是任何基础表。 (据推测,如果BDT$ACTBDT$HIST中的插入没有被注释掉,你真的想要删除旧的BDT$ACT记录吗?或者两者兼而有之 - 可能最终通过视图删除。 )

由于触发器仅为instead of insert,因此无法实现;但任何更新或删除视图的尝试都会出错,因为触发器不会覆盖其中任何一个操作。

您可以通过更改触发器来允许更新和删除(需要when deleting部分:

instead of insert or update or delete
on LCM_BDT