动态查询在所有 table oracle 中查找所有 table DML activity

Dynamic query to find all the table DML activity in all the table oracle

您好,我需要搜索架构中指定表列表的 DML activity

单个查询

select max(ora_rowscn),SCN_TO_TIMESTAMP(max(ora_rowscn)) FROM 'TABLE_NAME'

因为 运行 单个查询既费时又困难,所以我正在尝试准备动态 sql 来获取 max(ora_rowscn),SCN_TO_TIMESTAMP (max(ora_rowscn)) 来自所有表,以便我可以使用过滤器和 select 一组表

查询模板

select 'with tmp(table_name, row_number) as (' from dual 
union all 
select 'select '''||table_name||''',count(*) from '||table_name||' union  ' from USER_TABLES 
union all
select 'select '''',0 from dual) select table_name,row_number from tmp order by row_number desc ;' from dual;

我如何对所有表格使用 max(ora_rowscn),SCN_TO_TIMESTAMP(max(ora_rowscn))

有任何纠正查询语法的建议吗?

这是我的建议。我不建议对所有表使用 SCN_TO_TIMESTAMP,因为它会抛出很多 ORA-01405。

select 'with tmp(table_name, max_rscn, ct) as (' from dual 
union all 
select 'select '''||table_name||''',max(ora_rowscn), count(*) from '||table_name||' union  ' from USER_TABLES 
union all
select 'select '''',0,0 from dual) select table_name, max_rscn from tmp;' from dual;

您可以使用包含 EXECUTE IMMEDIATE 的 PLSQL 代码,以便通过 Dynamic SQL

获得所需的值
SET SERVEROUTPUT ON

DECLARE        
  v_rowscn NUMBER;
  v_tmstp  TIMESTAMP;
BEGIN
 FOR c IN 
 (SELECT t.table_name FROM user_tables t)
 LOOP
  BEGIN 
    EXECUTE IMMEDIATE 'SELECT max(ora_rowscn),SCN_TO_TIMESTAMP(max(ora_rowscn)) FROM '||
                               c.table_name INTO v_rowscn, v_tmstp;
    DBMS_OUTPUT.PUT_LINE( c.table_name||' - max_scn : '|| v_rowscn||
                         ' - max_scn_timestamp : '|| v_tmstp );
   EXCEPTION WHEN others THEN  DBMS_OUTPUT.PUT_LINE( sqlerrm );
  END;
 END LOOP;
END;
/ 

只要每个人不出现任何异常table。