当有人改变Oracle表时如何显示sql语句

时间:2018-04-09 01:54:49

标签: oracle

我有很多人使用的Oracle数据库。我想确切地知道改变表时人们做了什么并将其插入到日志历史表中。

2 个答案:

答案 0 :(得分:1)

这样的事情应该让你开始

DROP TABLE DDL_LOG CASCADE CONSTRAINTS
/

CREATE TABLE DDL_LOG
(
  TSTAMP       TIMESTAMP(6)                     NOT NULL,
  HOST         VARCHAR2(100),
  IP_ADDRESS   VARCHAR2(100),
  MODULE       VARCHAR2(100),
  OS_USER      VARCHAR2(100),
  TERMINAL     VARCHAR2(100),
  OPERATION    VARCHAR2(100),
  OWNER        VARCHAR2(50),
  OBJECT_NAME  VARCHAR2(50),
  OBJECT_TYPE  VARCHAR2(50),
  SQLTEXT      CLOB,
  PREVSQLTEXT  CLOB
)
/


DROP TRIGGER capture_all_ddl
/

CREATE OR REPLACE TRIGGER CAPTURE_ALL_DDL
after create or alter or drop on database
begin
    insert into ddl_log
    values (systimestamp,
                sys_context('USERENV','HOST'),
                sys_context('USERENV','IP_ADDRESS'),
                sys_context('USERENV','MODULE'),
                sys_context('USERENV','OS_USER'),
                sys_context('USERENV','TERMINAL'),
                ora_sysevent,
                ora_dict_obj_owner,
                ora_dict_obj_name,
                ora_dict_obj_type,
                case when ora_dict_obj_type not in ('PACKAGE','PROCEDURE','FUNCTION','PACKAGE BODY') and ora_sysevent != 'DROP' then
                  ( select sql_fulltext from v$sql
                    where sql_id = ( select sql_id from v$session where sid = sys_context('USERENV','SID') and rownum = 1 )
                  )
                end,
                case when ora_dict_obj_type not in ('PACKAGE','PROCEDURE','FUNCTION','PACKAGE BODY') and ora_sysevent != 'DROP' then
                  ( select sql_fulltext from v$sql
                    where sql_id = ( select prev_sql_id from v$session where sid = sys_context('USERENV','SID') and rownum = 1 )
                  )
                end
           );
     commit;
end;
/

答案 1 :(得分:0)

您可以使用AUDIT捕获和写入日志。