Oracle Trigger Typical Examples

前端之家收集整理的这篇文章主要介绍了Oracle Trigger Typical Examples前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  • Sync table A to Table B
CREATE@H_404_10@ TABLE@H_404_10@ A(ID INT@H_404_10@,DESCRIPTION VARCHAR@H_404_10@(10@H_404_10@));@H_404_10@
CREATE@H_404_10@ TABLE@H_404_10@ B(ID INT@H_404_10@,DESCRIPTION VARCHAR@H_404_10@(10@H_404_10@));@H_404_10@

CREATE@H_404_10@ OR@H_404_10@ REPLACE@H_404_10@ TRIGGER@H_404_10@ SYNC_A_TO_B AFTER@H_404_10@ INSERT@H_404_10@ OR@H_404_10@ UPDATE@H_404_10@ OR@H_404_10@ DELETE@H_404_10@ ON@H_404_10@ A FOR@H_404_10@ EACH@H_404_10@ ROW@H_404_10@ --indicate that this is@H_404_10@ a row@H_404_10@ trigger@H_404_10@,not@H_404_10@ table@H_404_10@ level@H_404_10@ trigger@H_404_10@ BEGIN@H_404_10@ DBMS_OUTPUT.PUT_LINE(:NEW.ID || '-'@H_404_10@ || :NEW.DESCRIPTION);@H_404_10@
  DBMS_OUTPUT.PUT_LINE(:OLD.ID || '-' || :OLD.DESCRIPTION);
  CASE
    WHEN INSERTING THEN 
      DBMS_OUTPUT.PUT_LINE('INSERTING');
      INSERT@H_404_10@ INTO@H_404_10@ B VALUES@H_404_10@ (:NEW.ID,:NEW.DESCRIPTION);@H_404_10@
    WHEN UPDATING THEN
      DBMS_OUTPUT.PUT_LINE('UPDATING');
      UPDATE@H_404_10@ B SET@H_404_10@ B.DESCRIPTION = :NEW.DESCRIPTION WHERE@H_404_10@ B.ID = :NEW.ID;@H_404_10@
    WHEN DELETING THEN
      DBMS_OUTPUT.PUT_LINE('DELETING');
      DELETE@H_404_10@ FROM@H_404_10@ B WHERE@H_404_10@ B.ID = :OLD.ID;@H_404_10@
  END@H_404_10@ CASE@H_404_10@;@H_404_10@
END@H_404_10@;@H_404_10@
/

INSERT@H_404_10@ INTO@H_404_10@ A VALUES@H_404_10@ (1@H_404_10@,'A_TO_B'@H_404_10@);@H_404_10@

SELECT@H_404_10@ * FROM@H_404_10@ A;@H_404_10@
SELECT@H_404_10@ * FROM@H_404_10@ B;@H_404_10@

UPDATE@H_404_10@ A SET@H_404_10@ A.DESCRIPTION = 'UPDATED'@H_404_10@ WHERE@H_404_10@ A.ID = 1@H_404_10@;@H_404_10@

SELECT@H_404_10@ * FROM@H_404_10@ A;@H_404_10@
SELECT@H_404_10@ * FROM@H_404_10@ B;@H_404_10@

DELETE@H_404_10@ FROM@H_404_10@ A WHERE@H_404_10@ A.ID = 1@H_404_10@;@H_404_10@

SELECT@H_404_10@ * FROM@H_404_10@ A;@H_404_10@
SELECT@H_404_10@ * FROM@H_404_10@ B;@H_404_10@

DROP@H_404_10@ TABLE@H_404_10@ A;@H_404_10@
DROP@H_404_10@ TABLE@H_404_10@ B;@H_404_10@
原文链接:https://www.f2er.com/oracle/211501.html

猜你在找的Oracle相关文章