Sunteți pe pagina 1din 3

code for listing out the schema tables sorted descendingly by the last

change timestamp for each table.


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

This could intensively help some users/DBA's to monitor the usage of his custom tables(Usage here means the
Updates and Inserts against these tables) and to get a primary estimation on the most and least accessed table on
his application and consequently can enhance his maintenance plan.

Solution
Here is a PLSQL code shows how to get a list of all the tables in a certain schema sorted descendingly by the last
change timestamp.

You can replace SCOTT and HR from the code by your schema or list of schemas you want to get the last
changes timestamps from these schema/schemas tables.

Note: The following code has not been created to be run against the default database dictionary user SYS.
Running the code against SYS user throw the following error:

ORA-08181: specified number is not a valid system change number


Index_organized tables are excluded from the output of this script.

The following inserts and updates on HR and Scott aims to make some changes against some tables on these
schemas because if we ran the code directly we might get all these schema tables with the same timestamp as all
of them has been created with the same timestamp when the database has first been created.
conn scott/tiger
update dept set loc='Cairo' where deptno=40;
commit;
update emp set deptno=30 where empno=7934;
commit;
conn hr/hr
update employees set email='tgates@hotmail' where employee_id=190;
commit;
update jobs set max_salary=10000 where job_id='ST_MAN';
commit;

Here is the actual code to be run against your schema or list of schemas:
conn / as sysdba
set linesize 10000
set serveroutput on

BEGIN
EXECUTE IMMEDIATE 'DROP TABLE table_xxx';
END;
/

BEGIN
EXECUTE IMMEDIATE 'CREATE TABLE table_xxx(table_name VARCHAR2(30), owner VARCHAR2(30), date
col TIMESTAMP)';
END;
/

DECLARE

CURSOR cur1 is SELECT table_name, owner


FROM dba_tables
WHERE owner in ('SCOTT','HR')
AND IOT_TYPE IS NULL ;
TYPE ref_cur IS REF CURSOR;
cur2 ref_cur;
v_table_name VARCHAR2(300);
v_owner VARCHAR2(300);
v_exec VARCHAR2(300);
v_exec1 VARCHAR2(300);
v_datecol TIMESTAMP;
v_len NUMBER;
v_spa VARCHAR2(500):='';
v_spa1 VARCHAR2(500):='';
v_count NUMBER :=1;

BEGIN

OPEN cur1;

LOOP
Fetch cur1 INTO v_table_name, v_owner;
v_exec1:='SELECT NVL(MAX(scn_to_timestamp(ora_rowscn)),SYSDATE-365) FROM ' ||
v_owner||'.'||v_table_name;
EXECUTE IMMEDIATE v_exec1 INTO v_datecol ;
v_exec := 'INSERT INTO table_xxx VALUES( :b2,:b3,:b4)';
EXECUTE IMMEDIATE v_exec USING v_table_name ,v_owner ,v_datecol;
EXIT WHEN cur1%NOTFOUND;
END loop;
CLOSE cur1;
COMMIT;

open cur2 for SELECT distinct table_name, owner,datecol


FROM table_xxx
WHERE datecol is not null
ORDER BY datecol desc ;

DBMS_OUTPUT.PUT_LINE('==================================================================');
DBMS_OUTPUT.PUT_LINE('Table Name Owner TimeStamp');
DBMS_OUTPUT.PUT_LINE('==================================================================');

LOOP

v_spa:=' ';
v_spa1:=' ';
Fetch cur2 INTO v_table_name, v_owner, v_datecol;

SELECT 30-TO_NUMBER(LTRIM(RTRIM(LENGTH(v_table_name))))
INTO v_len
FROM DUAL;
FOR i in 1..v_len LOOP
v_spa:=v_spa||' ';
END LOOP;

SELECT 15-TO_NUMBER(LTRIM(RTRIM(LENGTH(v_owner))))
INTO v_len
FROM DUAL;

FOR i in 1..v_len LOOP


v_spa1:=v_spa1||' ';
END LOOP;
EXIT WHEN cur2%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(v_table_name||v_spa||v_Owner||v_spa1||TO_CHAR(v_datecol,'DD/MM/RRRR
HH24:MI:SS') );

END LOOP;
DBMS_OUTPUT.PUT_LINE('==================================================================');
CLOSE cur2;
END;
/
.
.
The output should be like that:

==================================================================
Table Name Owner TimeStamp
==================================================================
JOBS HR 24/10/2008 19:20:10
EMPLOYEES HR 24/10/2008 19:18:16
EMP SCOTT 24/10/2008 19:06:43
DEPT SCOTT 24/10/2008 19:03:49
DEPARTMENTS HR 24/10/2008 18:50:01
JOB_HISTORY HR 24/10/2008 18:50:01
LOCATIONS HR 24/10/2008 18:50:01
REGIONS HR 24/10/2008 18:50:01
BONUS SCOTT 27/10/2007 18:54:18
SALGRADE SCOTT 30/08/2005 15:05:26
==================================================================

PL/SQL procedure successfully completed.

SQL>

S-ar putea să vă placă și