Sei sulla pagina 1di 3

How to capture the DDL statements executed in a database.

Applies to:
Oracle Server - Enterprise Edition - Version: 9.2.0.1 to 10.2.0.1
Information in this document applies to any platform.

Solution
The DDL statements exectued can be captured by creating a trigger as below.

1]. The parameter '_system_trig_enabled' needs to be set to true.

SQL> alter system set "_system_trig_enabled"=true scope=both;

SQL> show parameter system

NAME TYPE VALUE


------------------------------------ ----------- ------
_system_trig_enabled boolean TRUE

2]. Perform the below steps logged in as SYS.

CREATE TABLE DDL_ACTIONS


(
counter number(38)
,user_name VARCHAR2(4000)
,ddl_date VARCHAR2(26)
,ddl_type VARCHAR2(4000)
,object_type VARCHAR2(4000)
,owner VARCHAR2(4000)
,object_name VARCHAR2(4000)
,sqltext CLOB
);

create or replace trigger DDLTrigger


AFTER DDL ON DATABASE
declare
l_cnt BINARY_INTEGER := 0;
l_len integer := 0;
l_no integer := 1;
l_s varchar2(32767) := '';
l_sql_text ora_name_list_t;
BEGIN
l_cnt := ora_sql_txt(l_sql_text);
for i in 1..l_cnt
loop
if l_cnt = 1
then
insert into
DDL_ACTIONS
(
counter
,user_name
,ddl_date
,ddl_type
,object_type
,owner
,object_name
,sqltext
)
VALUES
(
i
,ora_login_user
,to_char(systimestamp,'yyyy.mm.dd hh24:mi:ss.FF')
,ora_sysevent
,ora_dict_obj_type
,ora_dict_obj_owner
,ora_dict_obj_name
,l_sql_text(i)
);
else
if l_len + length(l_sql_text(i)) > 32767
then
-- insert
insert into
DDL_ACTIONS
(
counter
,user_name
,ddl_date
,ddl_type
,object_type
,owner
,object_name
,sqltext
)
VALUES
(
l_no
,ora_login_user
,to_char(systimestamp,'yyyy.mm.dd hh24:mi:ss.FF')
,ora_sysevent
,ora_dict_obj_type
,ora_dict_obj_owner
,ora_dict_obj_name
,l_s
);
l_len := length(l_sql_text(i));
l_s := l_sql_text(i);
l_no := l_no + 1;
else
-- concat
l_s := l_s||l_sql_text(i);
l_len := l_len + length(l_sql_text(i));
end if;
end if;
end loop;
if l_s != ''
then
insert into
DDL_ACTIONS
(
counter
,user_name
,ddl_date
,ddl_type
,object_type
,owner
,object_name
,sqltext
)
VALUES
(
l_no
,ora_login_user
,to_char(systimestamp,'yyyy.mm.dd hh24:mi:ss.FF')
,ora_sysevent
,ora_dict_obj_type
,ora_dict_obj_owner
,ora_dict_obj_name
,l_s
);
end if;
END;
/

3]. Whenever an ddl statement is issued this will be captured in the table DDL_ACTIONS.

Example:

SQL> select * from ddl_actions;


no rows selected

SQL> create table dummy1 (abc number );


Table created.

SQL> alter table dummy1 add constraint abc_uk unique(abc);


Table altered.

SQL> select CONSTRAINT_NAME,CONSTRAINT_TYPE,TABLE_NAME from dba_constraints where


TABLE_NAME='DUMMY1';

CONSTRAINT_NAME C TABLE_NAME
------------------------------ - -----------
ABC_UK U DUMMY1

SQL> select COUNTER,DDL_TYPE,OBJECT_TYPE,SQLTEXT from ddl_actions;

COUNTER DDL_TYPE OBJECT_TYPE SQLTEXT


--------------------------------------------------------------------------------
1 CREATE TABLE create table dummy1 (abc number )

1 ALTER TABLE alter table dummy1 add constraint abc_uk unique(abc)

Potrebbero piacerti anche