PLS-00049:使用时:错误{NEW

时间:2019-04-11 00:07:59

标签: oracle plsql triggers

我有两个桌子

A(#ref_medic,libelle, vignette,remarque, qtestock)
B(#ref_medic, #dateF, qte_lot, unite, remarque)

如果要在A中存在ref_medic行,而在B中不存在行,我想使用插入触发器代替B行中的插入。

我不断收到错误消息:

  

PLS-00049:绑定变量'NEW.REF_MEDIC'错误

每当我在此视图中插入时:

感谢INSTEAD OF INSERT TRIGGER,这就是我要插入的视图

CREATE VIEW myview(reference,libelle,vignette, date_peremption, Quantite,unite,
    Remarque) AS
    SELECT M.ref_medic, libelle, vignette, dateF,Qte_lot,unite,LM.remarque
    FROM Medicament M, Lot_medicament LM
    WHERE M.ref_medic=LM.ref_medic
    AND qte_lot>0;

我尝试删除:NEW,但这不是我希望做的事,因为我想将此行插入B:

INSERT INTO myview VALUES
('12AS45','test','yes','06/06/2021',30,'boite','test');
  

PLS-00049:变量Attachée(绑定变量)错误'NEW.REF_MEDIC'

我们应该得到以下结果:该行将不存在于B中,因为它在那里存在

2 个答案:

答案 0 :(得分:1)

您的列名中是否有一个“#”?可能会引起问题。

否则它将正常工作。

这是一个经过测试的示例:

create table Medicament(
ref_medic VARCHAR2(10),
libelle VARCHAR2(30),
vignette VARCHAR2(3),
remarque VARCHAR2(50),
qtestock NUMBER);

create table Lot_medicament(
ref_medic VARCHAR2(10),
dateF DATE,
qte_lot NUMBER,
unite VARCHAR2(15),
remarque VARCHAR2(50));

CREATE VIEW myview(reference,libelle,vignette, date_peremption, Quantite,unite, Remarque) AS
  SELECT M.ref_medic, libelle, vignette, dateF, Qte_lot, unite, LM.remarque
  FROM Medicament M, Lot_medicament LM
  WHERE M.ref_medic = LM.ref_medic
  AND qte_lot > 0;

CREATE OR REPLACE TRIGGER instead_insert_in_myview
INSTEAD OF INSERT ON myview
DECLARE
  med_ref  Medicament.ref_medic%TYPE;
BEGIN
  BEGIN
    SELECT ref_medic into med_ref FROM Medicament where ref_medic = :new.reference;
  EXCEPTION
   WHEN NO_DATA_FOUND THEN
     med_ref := NULL;
  END;
  IF med_ref IS NULL THEN
    INSERT INTO Medicament(ref_medic, libelle, vignette, remarque, qtestock)
     VALUES (:new.reference, :new.libelle, :new.vignette, :new.remarque, :new.Quantite);
  END IF;
  INSERT INTO Lot_medicament(ref_medic, dateF, qte_lot, unite, remarque)
   VALUES (:new.reference, :new.date_peremption, :new.Quantite, :new.unite, :new.remarque);
END instead_insert_in_myview;
/

INSERT INTO myview VALUES
('12AS45','test','yes','06/06/2021',30,'boite','test');

然后

select count(*) from Medicament;

给您一行,并且

select count(*) from Lot_medicament;

还会给您一行。

答案 1 :(得分:0)

错误是在触发器内编写:NEW.ref_med 而不是 :NEW.reference

谢谢大家。