Warm tip: This article is reproduced from serverfault.com, please click

Oracle ddl trigger on database

发布于 2020-11-30 09:07:21

I have question related to my ddl trigger:

create or replace trigger audit_ddl_db_trg after ddl on database
begin
DECLARE
    n number;
    stmt     varchar2(4000);
    sql_text ora_name_list_t;
  begin
      dbms_output.put_line(ora_sql_txt(sql_text));
          n := ora_sql_txt(sql_text);
          IF nvl(n,200)=200 THEN
                           Raise_application_error(-20001, 'ora_sql_txt does not catch any statement. sql_txt is not initialized');
          ELSE FOR i IN 1..n LOOP
                    stmt := stmt || sql_text(i);
            END LOOP;
            dbms_output.put_line(stmt);
          END IF;

    IF ora_sysevent <> 'TRUNCATE'
    THEN
  if (ora_sysevent='TRUNCATE')
  then
    null; -- I do not care about truncate
  else
    insert into audit_ddl(date_of_change,osuser,current_user,host,terminal,owner,object_type,object_name,ora_sysevent,sqltext)
    values(
      systimestamp,
      sys_context('USERENV','OS_USER') ,
      sys_context('USERENV','CURRENT_USER') ,
      sys_context('USERENV','HOST') ,
      sys_context('USERENV','TERMINAL') ,
      ora_dict_obj_owner,
      ora_dict_obj_type,
      ora_dict_obj_name,
      ora_sysevent,
      stmt
    );
    end if;
    end if;
    end;
end;

Trigger catches everything correctly, i do have everything populated well in my table, but i do have questions related to column current_user. No matter which user schema i use in database to do alter it writes SYS. How to populate this with correct user which has done alter on table?

Thanks!

Questioner
user3391373
Viewed
0
Popeye 2020-11-30 17:25:40

sys_context('USERENV','CURRENT_USER') returns The name of the database user whose privileges are currently active, as per the oracle documentation.

You can use the SESSION_USER instead of CURRENT_USER.