Oracle中利用Trigger进行工作

本文介绍了一个具体的Oracle数据库触发器案例,用于记录对特定表进行插入、更新和删除操作的日志,并将这些操作的历史记录保存到另一个表中。通过此触发器,可以轻松追踪数据的变化历史。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Oracle数据库中,有些情况下,对数据记录需要记录日志,或保存操作历史等情况.在此我们可以借助“数据库Trigger”进行。下面以一例进行说明:

 

CREATE OR REPLACE TRIGGER aits_auth_group_auth_trga_diu
  AFTER update OR DELETE OR  INSERT on  aits_authority_group_auth
  for each row
/*****************************************************************************************
*
* Trigger Name           : aits_auth_group_auth_trga_diu
* Description            : Log insert, Modify ,delete action to History table
* Version                : 1.0

*****************************************************************************************/

 

 

 


declare

BEGIN

   IF inserting THEN
       INSERT INTO aits_auth_group_auth_history
         (application_code, auth_id, ranage_code,
          group_code, module_code, function_code,
          creation_date, created_by, last_update_date,
          last_updated_by, action_type)
       VALUES
         (:new.application_code, :new.auth_id, :new.ranage_code,
          :new.group_code, :new.module_code, :new.function_code,
          :new.creation_date, :new.created_by, :new.last_update_date,
          :new.last_updated_by, aits_pak_common.InsertDesc);
   ELSIF DELETING THEN
       INSERT INTO aits_auth_group_auth_history
         (application_code, auth_id, ranage_code,
          group_code, module_code, function_code,
          creation_date, created_by, last_update_date,
          last_updated_by, action_type)
       VALUES
         (:OLD.application_code, :OLD.auth_id, :OLD.ranage_code,
          :OLD.group_code, :OLD.module_code, :OLD.function_code,
          :OLD.creation_date, :OLD.created_by, :OLD.last_update_date,
          :OLD.last_updated_by, aits_pak_common.DeleteDesc);
   ELSIF UPDATING  THEN
       IF UPDATING('application_code') OR UPDATING('auth_id') OR UPDATING('ranage_code')
          OR UPDATING('group_code') OR UPDATING('module_code') OR UPDATING('function_code')   THEN
             INSERT INTO aits_auth_group_auth_history
               (application_code, auth_id, ranage_code,
                group_code, module_code, function_code,
                creation_date, created_by, last_update_date,
                last_updated_by, action_type)
             VALUES
               (:new.application_code, :new.auth_id, :new.ranage_code,
                :new.group_code, :new.module_code, :new.function_code,
                :new.creation_date, :new.created_by, :new.last_update_date,
                :new.last_updated_by, aits_pak_common.UpdateDesc);
       END IF;
  END IF;
end aits_auth_group_auth_trga_diu;

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值