Problems changing the date with a trigger in oracle

后端 未结 1 1636
情书的邮戳
情书的邮戳 2021-01-26 08:46

I want to create a trigger that can modify or insert a date in my table \"contracts\":

\"If a client with a current contract signs a new one, the end date Of the previou

相关标签:
1条回答
  • 2021-01-26 09:15

    You want something like this:

    CREATE OR REPLACE TRIGGER TRIGGER_D
    BEFORE
    INSERT OR UPDATE ON CONTRACTS
    FOR EACH ROW
    DECLARE
    BEGIN
      UPDATE CONTRACTS
      SET    ENDDATE   = :NEW.STARTDATE - INTERVAL '1' DAY
      WHERE  CLIENTID  = :NEW.CLIENTID
      AND    ENDDATE   > :NEW.STARTDATE
      AND    STARTDATE < :NEW.STARTDATE;
    END;
    

    However, it is updating the table that the trigger is monitoring and it seems likely that the trigger will cause updates that will invoke the trigger again and get a mutating table error.

    You might be better to wrap your business logic in a stored procedure where it can perform the update on the previous rows and then do the insert/update. Then, rather than performing DML statements directly on the table instead invoke the stored procedure.

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