Oracle Trigger仍然为New提供旧值

时间:2013-09-25 16:00:01

标签: oracle plsql triggers

在用户通过在线应用程序更新ADDRESS表后,我写了一些触发器来自动更改ESTABLISHMENT表中state_id字段的值。问题是,当用户在地址中更新OTHER_STATE_ID = 16和POSTAL_STATE_ID = 16时,表ESTABLISHMENT中OTHER_STATE_ID的值仍然带有旧值。仅使用新值更新POSTAL_STATE_ID字段。我检查了触发器代码,发现没有奇怪的代码。似乎触发器保留了更新other_state_id的旧值。以下是数据方案: -

  1. 更新前(ADDRESS表)

    • 对于ADDRESS_TYPE = 1,值LOCATION_STATE_ID = 8
    • 对于ADDRESS_TYPE = 2,值POSTAL_STATE_ID = 8
    • 对于ADDRESS_TYPE = 11,值OTHER_STATE_ID = 8

    触发前(ESTABLISHMENT表)

    • 值LOCATION_STATE_ID = 8
    • 值POSTAL_STATE_ID = 8
    • 值OTHER_STATE_ID = 8
  2. 更新后的值(ADDRESS表)

    • 用户更新值LOCATION_STATE_ID = 8
    • 对于ADDRESS_TYPE = 2,值POSTAL_STATE_ID = 16
    • 对于ADDRESS_TYPE = 11,值OTHER_STATE_ID = 16

    触发后(ESTABLISHMENT表)

    • 值LOCATION_STATE_ID = 8
    • 值POSTAL_STATE_ID = 16
    • 值OTHER_STATE_ID = 8
  3. 在这行代码中可能会出现奇怪的事情。任何人..?

    完整代码如下

        create or replace
    TRIGGER "TR_ADDRESS_EE"
       AFTER INSERT OR UPDATE
       ON ADDRESS
       FOR EACH ROW
    BEGIN
       IF INSERTING
       THEN
          IF (:NEW.STATE_ID IS NOT NULL)
          THEN
             IF (:NEW.RECORD_TYPE_ID = 401)
             THEN
                IF (:NEW.ADDRESS_TYPE = 1)
                THEN
                   UPDATE ESTABLISHMENT
                      SET LOCATION_STATE_ID = :NEW.STATE_ID
                    WHERE ESTABLISHMENT_ID = :NEW.RECORD_ID;
                ELSIF (:NEW.ADDRESS_TYPE = 2)
                THEN
                   UPDATE ESTABLISHMENT
                      SET POSTAL_STATE_ID = :NEW.STATE_ID
                    WHERE ESTABLISHMENT_ID = :NEW.RECORD_ID;
                ELSIF (:NEW.ADDRESS_TYPE = 11)
                THEN
                   UPDATE ESTABLISHMENT
                      SET OTHER_STATE_ID = :NEW.STATE_ID
                    WHERE ESTABLISHMENT_ID = :NEW.RECORD_ID;
                END IF;
             END IF;
          END IF;
       ELSIF UPDATING ('STATE_ID')
       THEN
          IF (:OLD.RECORD_TYPE_ID = 401 AND :NEW.STATE_ID <> :OLD.STATE_ID)
          THEN
             IF (:NEW.ADDRESS_TYPE = 1)
             THEN
                UPDATE ESTABLISHMENT
                   SET LOCATION_STATE_ID = :NEW.STATE_ID
                 WHERE ESTABLISHMENT_ID = :OLD.RECORD_ID;
             ELSIF (:NEW.ADDRESS_TYPE = 2)
             THEN
                UPDATE ESTABLISHMENT
                   SET POSTAL_STATE_ID = :NEW.STATE_ID
                 WHERE ESTABLISHMENT_ID = :OLD.RECORD_ID;
             ELSIF (:NEW.ADDRESS_TYPE = 11)
             THEN
                UPDATE ESTABLISHMENT
                   SET OTHER_STATE_ID = :NEW.STATE_ID
                 WHERE ESTABLISHMENT_ID = :OLD.RECORD_ID;
             END IF;
          END IF;
       END IF;
    END;
    

0 个答案:

没有答案