【发布时间】:2017-06-08 14:06:41
【问题描述】:
我有this 数据库,我需要回答以下问题:
"您需要创建一个触发器以及触发触发器时将执行的函数。每次检查患者并进行诊断时(如果插入的约会.diagnosis 不为空),患者的医疗文件夹将被更新。”
这是我想出的:
CREATE OR REPLACE FUNCTION appointmentsFunction()
RETURNS TRIGGER AS $appointmentsfunction$
DECLARE
appointment_patientamka BIGINT := new.patientamka;
appointment_cure TEXT := new.cure;
appointment_drug_id INT := drug_id;
appointment_diagnosis TEXT := new.diagnosis;
BEGIN
IF appointment_diagnosis is NOT NULL THEN
INSERT INTO public.medicalfolder (patient,cure,drug_id) VALUES (appointment_patientamka,appointment_cure, appointment_drug_id);
END IF;
RETURN NEW;
END
$appointmentsfunction$
LANGUAGE plpgsql;
最佳实践/最佳方法是什么?
附言。 patientamka 用作患者的 ID。它也可以是 patientID,但根据我老师的指导,它不是。
PS2.Medicalfolder 表可以包含同一患者的重复记录。所以,我不必用病人的 id (patientamka) 更新记录,我只需要插入一个新的。
【问题讨论】:
标签: postgresql function triggers insert