Oracle触发器实现更新一张表数据时判断另一张表是否存在,存在更新数据,不存在插入数据

CREATE OR REPLACE TRIGGER TGR_NAME
AFTER UPDATE ON TABLE1_NAME
FOR EACH ROW
DECLARE
V_COUNT NUMBER(10);
BEGIN
SELECT COUNT(*) INTO V_COUNT FROM TABLE2_NAME WHERE ZJ=:NEW.ZJ;
IF V_COUNT = 0 THEN 
INSERT INTO TABLE2_NAME (ZJ,MC)
VALUES(:NEW.ZJ,:NEW.MC);
ELSE 
UPDATE TABLE2_NAME SET MC=:NEW.MC WHERE ZJ=:OLD.ZJ;
END IF;END;

你可能感兴趣的:(Oracle)