Oracle Query以获取先前更新的记录

时间:2019-06-24 17:13:53

标签: oracle

打开Oracle查询以获取具有先前更改的更改的详细信息, 说值X已更新为值Y,现在我需要帮助来形成查询以获取以前的记录X。

1 个答案:

答案 0 :(得分:0)

如果您确实需要根据数据库中所述记录内的字段中包含的数据来跟踪某些记录状态,触发器将允许您在将记录更新或删除到相应的历史记录之前将其移动表或称为审计表。这样的想法是,您然后将根据怀疑记录已更新的日期/时间查询审计表,以在更新该字段之前检索该字段的值。

示例UPDATE / DELETE触发器

TRIGGER user_schema.ACCOUNTS_HIST_TRIG
  AFTER
   DELETE OR UPDATE of account_bus_id, account_name, account_phone, other_field_you_care_about
  ON user_schema.accounts
 REFERENCING NEW AS NEW OLD AS OLD
  FOR EACH ROW

 DECLARE

   numUser number; /* Used to store user_id */   

 BEGIN

  /* Perhaps you have code to pull in the user that performed the update */
  numuser := user_schema.get_user_updated();

  INSERT INTO user_schema.accounts_hist(
    account_id,
    account_type,
    account_phone,
    account_phone_type,
    updated_by
  )
  VALUES(:OLD.account_id,
    :OLD.account_type,
    :OLD.account_phone,
    :OLD.account_phone_type,
    numuser);

exception
   WHEN others THEN
     RAISE;
 END;