更新B表后如何从A表获取一个属性值到B表?

时间:2019-04-27 10:37:10

标签: sql oracle plsql triggers

我有两个表:paperpaperselection

在我的代码中,我想将rank表中具有paper属性大于4的任何纸张放入paperselection表中

此后,我想做的是在将值插入papNo表中之后,将主键(paper)从该paperselection移到paperselection

我在这里有两个触发器

---first trigger---
CREATE OR REPLACE TRIGGER paperselection_tr
  BEFORE INSERT OR UPDATE OF Rank ON paper
  FOR EACH ROW
BEGIN
 IF :NEW.rank > 4
   THEN insert into paperselection (Papno, journalno, title, pdate)
values (:old.papno, :new.journalno, :new.title, :new.pdate);
END IF;
END;

---second trigger----
CREATE OR REPLACE TRIGGER paperselection_tr2
  After insert or update ON paperselection
  FOR EACH ROW
BEGIN
update paperselection
set paperselection.papno = paper.papno
where paper.title = paperselection.title;
end;

由于表的设计原因,我无法将:old.papno设置为:new.papno

但是它一直在提示我错误:

LINE/COL  ERROR
--------- -------------------------------------------------------------
2/1       PL/SQL: SQL Statement ignored
4/7       PL/SQL: ORA-00904: "PAPER"."TITLE": invalid identifier
Errors: check compiler log

1 个答案:

答案 0 :(得分:0)

第二个触发器对paper表一无所知,因为它触发了After insert or update ON paperselection,并且因为您在其中没有对paper表的进一步引用。

因此不喜欢这样:

set paperselection.papno = paper.papno
where paper.title = paperselection.title;

我认为你想要这个

---second trigger----
CREATE OR REPLACE TRIGGER paperselection_tr2
  After insert or update ON paperselection
  FOR EACH ROW
BEGIN
update paperselection
set paperselection.papno = 
   (SELECT paper.papno
    FROM paper    
    WHERE paper.title = :new.title
   );
end;

但是,只有在paperselection中有重复的行时,这才是逻辑操作。