Is there an automatic modification time stamp type for Oracle columns?

前端 未结 6 2128
臣服心动
臣服心动 2020-12-15 05:46

Is there a way to create a timestamp column in Oracle that automatically stores a timestamp of when the record has changed ?

6条回答
  •  眼角桃花
    2020-12-15 06:04

    Pretty sure you have to do this with a trigger in Oracle:

    create or replace TRIGGER parkedorder_tbiur
       BEFORE INSERT OR UPDATE
       ON parkedorder
       REFERENCING OLD AS old_row NEW AS new_row
       FOR EACH ROW
    BEGIN
       IF INSERTING
       THEN
          IF :new_row.ID IS NULL
          THEN
             SELECT parkedorder_seq.NEXTVAL
               INTO :new_row.ID
               FROM DUAL;
          END IF;
       END IF;
    
       IF    :new_row.lastupdated <> SYSDATE
          OR :new_row.lastupdated IS NULL
       THEN
          SELECT sysdate
            INTO :new_row.lastupdated
            FROM DUAL;
       END IF;
    
       SELECT SYS_CONTEXT ( 'USERENV', 'OS_USER' )
         INTO :new_row.lastupdatedby
         FROM DUAL;
    END;
    

提交回复
热议问题