【问题标题】:SqlPlus script to update data and log the update values in a CLOB用于更新数据并将更新值记录在 CLOB 中的 SqlPlus 脚本
【发布时间】:2021-11-22 03:22:53
【问题描述】:

我正在创建一个 shell 脚本,它使用 sqlplus 来更新 Oracle 数据库中的数据。

我有一个像这样的 user 表。

id_user name statut
1 aaa 1
2 bbb 1

还有另一个表 user_log 像这样。

date nb_rows_affected details

日志表包含列详细信息,这是一个 CLOB,我想在其中详细记录用户表中所做的所有更新。

我给你举个例子,假设我们在用户表中有上述信息,我们想将用户 aaa 的名称和状态更新为 user.name == ccc 并将他的状态更新为 user。状态=2 所以我会这样做

update user set statut = 2 and name = 'ccc' where id_user = 1 

我希望更新后自动在日志表中显示以下信息

date = current_date 
nbrow_affected = 1 
details : new values (statut = 2 and name = 'ccc') old values (statut = 1 and name = 'aaa') for id_user= 1

我一直在寻找它的唯一方法是在用户表更新时使用触发器,因为它可以使用 new.name 和 old.name 语句,但我不知道如何正确实现它

我该怎么做?

【问题讨论】:

    标签: sql oracle shell plsql sqlplus


    【解决方案1】:

    您可以构建一个询问每一列的触发器。它很冗长,但您也可以编写一个例程来自动从 USER_TAB_COLUMNS 中为您生成它

    触发代码如下:

    declare
      c clob;
    begin
      if updating(col1) then c := c || 'old='||:old.col1||',new='||:new.col1; end if;
      if updating(col2) then c := c || 'old='||:old.col2||',new='||:new.col2; end if;
      if updating(col3) then c := c || 'old='||:old.col3||',new='||:new.col3; end if;
      if updating(col4) then c := c || 'old='||:old.col4||',new='||:new.col4; end if;
      ...
      ...
    end;  
    

    最后,您会将 clob 'c' 存储在日志表中

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多