创建触发器时没有错误。但是,当我启动它时,错误消息显示表正在变异。如何解决表格变异的问题?
CREATE OR REPLACE TRIGGER tgr_invoice
AFTER UPDATE OF discharge_date
ON Admission
FOR EACH ROW
DECLARE
doc_fee NUMBER(5);
nights_stayed NUMBER(4);
room_price NUMBER(5);
room_fee NUMBER(9);
med_fee NUMBER(7, 2);
units_prescribed NUMBER(3);
price_per_unit NUMBER(7, 2);
total_fee NUMBER(8,2);
BEGIN
SELECT consult_fee INTO doc_fee
FROM Doctor
WHERE staff_id IN (SELECT seen_by FROM Admission
WHERE patient_id = :NEW.patient_id
AND admission_date = :NEW.admission_date);
SELECT (discharge_date - admission_date) INTO nights_stayed
FROM Admission
WHERE patient_id = :NEW.patient_id
AND admission_date = :NEW.admission_date;
SELECT room_price INTO room_price
FROM Room
WHERE room_no IN (SELECT room_no FROM Admission
WHERE patient_id = :NEW.patient_id
AND admission_date = :NEW.admission_date);
SELECT units_prescribed INTO units_prescribed
FROM Prescription
WHERE patient_id = :NEW.patient_id
AND prescription_date = :NEW.admission_date;
SELECT price_per_unit INTO price_per_unit
FROM Medicine
WHERE med_id IN (SELECT med_id FROM Prescription
WHERE patient_id = :NEW.patient_id
AND prescription_date = :NEW.Admission_date);
room_fee := room_price * nights_stayed;
med_fee := units_prescribed * price_per_unit;
total_fee := room_fee + doc_fee + med_fee;
INSERT INTO Invoice VALUES(seq_inv_no.NEXTVAL, :NEW.patient_id,
:NEW.admission_date, doc_fee, room_fee, med_fee, total_fee);
END;
/
当我更新入场表并输入出院日期时,出现错误“表SYSTEM.ADMISSION正在变异,触发器/功能可能看不到它”
答案 0 :(得分:0)
我认为,通过在创建DB触发器的表的列的:NEW或:OLD值之间进行操作而从分配中派生值会更合适,而不是对行级触发器使用SELECT语句。因此,请考虑使用以下代码块:
CREATE OR REPLACE TRIGGER tgr_invoice AFTER UPDATE OF discharge_date ON Admission
FOR EACH ROW
DECLARE
doc_fee NUMBER(5);
nights_stayed NUMBER(4);
room_price NUMBER(5);
room_fee NUMBER(9);
med_fee NUMBER(7, 2);
units_prescribed NUMBER(3);
price_per_unit NUMBER(7, 2);
total_fee NUMBER(8,2);
BEGIN
SELECT consult_fee
INTO doc_fee
FROM Doctor
WHERE staff_id = :NEW.seen_by;
nights_stayed := :NEW.discharge_date - :NEW.admission_date;
SELECT room_price
INTO room_price
FROM Room
WHERE room_no = :NEW.room_no;
SELECT units_prescribed
INTO units_prescribed
FROM Prescription
WHERE patient_id = :NEW.patient_id
AND prescription_date = :NEW.admission_date;
SELECT price_per_unit INTO price_per_unit
FROM Medicine
WHERE med_id IN (SELECT med_id
FROM Prescription
WHERE patient_id = :NEW.patient_id
AND prescription_date = :NEW.Admission_date);
room_fee := room_price * nights_stayed;
med_fee := units_prescribed * price_per_unit;
total_fee := room_fee + doc_fee + med_fee;
INSERT INTO Invoice
VALUES(seq_inv_no.NEXTVAL, :NEW.patient_id, :NEW.admission_date,
doc_fee, room_fee, med_fee, total_fee);
END;
/