使用默认值更新列

Updating Columns with Default Value

我有一个基于 table 的 editable 交互式网格。我想用更改日期更新 MODIFIED_DTS,并且 MODIFIED_BY_USR_ID 以在用户对 table 进行任何更改时自动捕获谁进行了更改。

有什么办法可以实现吗?

是的,绝对有办法实现这一点。到目前为止,最好的方法是在您的交互式网格所基于的 table 上创建触发器。 生成此触发器的最简单方法是使用 livesql.oracle.com 上可用的 quicksql。它生成这样的触发器:

create or replace trigger <your_table_name>_BIU
    before insert or update 
    on <your_table_name>
    for each row
begin
    if inserting then
        :new.created := sysdate;
        :new.created_by := nvl(sys_context('APEX$SESSION','APP_USER'),user);
    end if;
    :new.updated := sysdate;
    :new.updated_by := nvl(sys_context('APEX$SESSION','APP_USER'),user);
end tp_item_biu;
/ 

将 your_table_name 和 "created, created_by, updated, updated_by" 替换为您的列名,编译即可。

是的,您可以为此使用数据库 table 触发器。优点是它可以在您的应用程序中任何修改 table 的地方工作,此外,如果您的应用程序之外的人修改了 table,它也可以工作。合并将首先查看 insert/update 是否在 APEX 上下文中执行并使用 app_user,如果不是,则使用当前数据库用户。

create or replace trigger my_trigger_name
    before insert or update on my_table
    for each row
begin
    :new.modified_dts        := sysdate;
    :new.modified_by_user_id := coalesce(sys_context('APEX$SESSION', 'APP_USER'), user);
end;
/