SQL Raise Application Error Trigger

后端 未结 1 2020
灰色年华
灰色年华 2021-01-23 15:19

This is throwing \"Error: ORA-04082: NEW or OLD references not allowed in table level triggers\"

I\'m not sure where I\'m going wrong. The error number shouldn\'t make a

相关标签:
1条回答
  • 2021-01-23 16:06

    As the error suggests, you can only refer to the new and old pseudo-rows in a row-level trigger, not a table-level trigger, which fires once regardless of how many rows were affected by the statement. If you updated two rows with different drinker IDs, which value would the trigger use for its look-up?

    To make it a row-level trigger, add FOR EACH ROW:

    CREATE OR REPLACE TRIGGER REJECTION 
    BEFORE INSERT OR UPDATE ON TEA_PREFS_T 
    FOR EACH ROW
    DECLARE
      temp NUMBER;
    BEGIN
      SELECT COUNT(*) INTO temp FROM tea_prefs_t WHERE person = :new.drinkerid;
      IF (temp >=10) THEN
        raise_application_error(-20101, 'ERROR: CANNOT INSERT MORE THAN 10');
      END IF;
    END;
    /
    

    You can't commit or rollback from within a trigger; it's up to the transaction doing the insert/update to decide whether to do that.

    However, you also can't select from the same table you're inserting into/updating; you'll get a mutating table error from this, at least if you attempt to insert/update multiple rows at once.

    0 讨论(0)
提交回复
热议问题