查找某个表的数据来源 - ORACLE

4

这可能是一个微不足道的问题。但是,由于我正在处理由他人创建的数据库,长时间没有适当的文档或注释,我遇到了一个关键问题,我需要知道数据如何插入到某个表中?是否有脚本或其他方法可以识别数据源?换句话说,我需要知道数据是通过某些过程、函数、手动...等方式插入的。我不能搜索所有的过程或函数,它们有数百个。我正在使用SQL Developer和它的Oracle 11g数据库。

2个回答

6

没有相应的脚本可以确定数据表的来源。最好的方法是筛选所有引用了该表的存储过程。

SELECT *
  FROM dba_dependencies
 WHERE referenced_owner = 'SCOTT'
   AND referenced_name  = 'YOUR_TABLE_NAME'
   AND referenced_type  = 'TABLE'

或者您可以使用这个脚本。
SELECT *
  FROM dba_source
 WHERE UPPER(text) LIKE '%YOUR_TABLE_NAME%';

这将过滤掉所有引用了您的表的存储过程/触发器/其他数据库代码,然后您需要检查哪些代码中使用了您的insert语句。


2

在表上添加一个触发器,记录PL/SQL调用堆栈信息,使用dbms_utility.format_call_stack()可以提供所需的信息;以下是一个示例,用于记录所有对INS_TEST表的插入操作(日志包含在INS_LOG中):

create table ins_test (pk number not null primary key);

create table ins_log(pk number not null primary key,
  text varchar2(4000));

create sequence seq_ins;
create sequence seq_log;  

create or replace trigger tr_air_ins_test after insert on ins_test
for each row
begin
  insert into ins_log(pk, text) values (
    seq_log.nextval, 
    dbms_utility.format_call_stack
  );
end;

create or replace procedure proc1 as
begin
  insert into ins_test values (seq_ins.nextval);
end;

create or replace procedure proc2 as
begin
  insert into ins_test values (seq_ins.nextval);
end;

begin
  proc1;
  proc2;
end; 

insert into ins_test values (seq_ins.nextval);

但在使用此功能之前,您应该运行R.T.建议的SQL语句-这样做更加简单,不会影响您的数据库,并且可能已经足够了。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接