我为数据库中的表创建了一个更新后触发器,当在特定列中修改某个值时,它还会从另一个数据库更新同一个表。我的问题是,我如何检查结果是否为0(这意味着第二个数据库中不存在这样的行,1表示该行存在及其更新)并相应地采取行动?我想如果我得到0表示这样的主键不存在,在第二个数据库中创建它。这是我的ORACLE触发器代码:
CREATE OR REPLACE TRIGGER UPDATE_BIOGR
AFTER UPDATE OF ID_PATIENTS,NAME,SNAME,GENDER,DATE_BORN,COUNTRY,CITY,BLOODTYPE,FATHER_ID,MOTHER_ID,FATHER_NAME,FATHER_LASTNAME,MOTHER_FIRSTNAME,MOTHER_LASTNAME,TIL,E_MAIL ON BIOGR
REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW
DECLARE
v_username varchar2(10);
BEGIN
SELECT user INTO v_username
FROM dual;
UPDATE PERSONAL_RECORD.BIOGR SET
ID = :NEW.ID_PATIENTS, NAME = :NEW.NAME, SNAME = :NEW.SNAME, GENDER = :NEW.GENDER, DATEBORN = :NEW.DATE_BORN, PLACE_BORN = :NEW.COUNTRY,
PLACE_STATE = :NEW.CITY, BLOOD_TYPE = :NEW.BLOODTYPE, ID_FATHER = :NEW.FATHER_ID, ID_MOTHER = :NEW.MOTHER_ID,
FATHER_NAME = :NEW.FATHER_NAME, FATHER_SNAME = :NEW.FATHER_LASTNAME, MOTHER_NAME = :NEW.MOTHER_FIRSTNAME,
MOTHER_SNAME = :NEW.MOTHER_LASTNAME, TIL = :NEW.TIL, EMAIL = :NEW.E_MAIL, UPDATED_FROM = v_username
WHERE PERSONAL_RECORD.BIOGR.ID = :NEW.ID_PATIENTS;
END;
答案 0 :(得分:0)
尝试MERGE - 语句,如下所示:
MERGE INTO personal_record.biogr USING dual ON
(PERSONAL_RECORD.BIOGR.ID = :OLD.ID_PATIENTS)
WHEN MATCHED THEN
UPDATE SET
ID = :NEW.ID_PATIENTS, NAME = :NEW.NAME, SNAME = :NEW.SNAME, GENDER = :NEW.GENDER, DATEBORN = :NEW.DATE_BORN, PLACE_BORN = :NEW.COUNTRY,
PLACE_STATE = :NEW.CITY, BLOOD_TYPE = :NEW.BLOODTYPE, ID_FATHER = :NEW.FATHER_ID, ID_MOTHER = :NEW.MOTHER_ID,
FATHER_NAME = :NEW.FATHER_NAME, FATHER_SNAME = :NEW.FATHER_LASTNAME, MOTHER_NAME = :NEW.MOTHER_FIRSTNAME,
MOTHER_SNAME = :NEW.MOTHER_LASTNAME, TIL = :NEW.TIL, EMAIL = :NEW.E_MAIL, UPDATED_FROM = v_username
WHEN NOT MATCHED THEN
INSERT (id, name, sname, ....)
VALUES (:NEW.ID_PATIENTS, :NEW.NAME, :NEW.SNAME, ...);