【问题标题】:Oracle - how to find columns in a table + stored procedures dependent on them?Oracle - 如何在表中查找列+依赖于它们的存储过程?
【发布时间】:2009-10-01 03:00:58
【问题描述】:

场景:

我需要列出table1 中的所有列以及依赖于table1 的这些列的所有存储过程。我需要将列名和存储过程填充到新表中。

我创建了new_table(col1, sProc) 并尝试在此new_table 上填充列名和相应的存储过程。我写的代码如下:

Declare

Begin

for i in (select column_name from user_tab_columns where lower(table_name) like 'table1') loop

insert into new_table
  select i.column_name, 
        name 
   from user_source 
  where lower(text) like '%' || i.column_name || '%';

commit;

end loop;

end;

结果: 脚本成功运行,但此 new_table 上未填充任何数据。

压力: 我试图解决它一整天,但无法弄清楚。对此的任何帮助将不胜感激。再一次感谢你。

【问题讨论】:

  • FOR 循环不是必需的,而且您链接两个表的条​​件不正确。

标签: sql oracle plsql


【解决方案1】:

一个明显的问题是您将过程文本转换为小写,而不是您要查找的列名。

但是,此代码还有其他问题。如果列名恰好与不是列引用的文本的某些部分匹配,会发生什么情况?

【讨论】:

    【解决方案2】:

    您能做的最好的事情是列出包名称(因为这是USER_SOURCE.NAME 字段中的值)以及列。正如 rexem 在他的评论中指出的那样,您不需要诉诸 for 循环:

     INSERT INTO new_table (col1, sproc) 
        SELECT i.column_name, u.name 
        FROM user_tab_columns i, 
             user_source u 
        WHERE lower(i.table_name) like 'table1' 
          AND lower(u.text) like '%' || lower(i.column_name) || '%';
    

    【讨论】:

      【解决方案3】:

      您可以通过在查询中包含USER_DEPENDENCIES 来减少误报。您可能想要限制搜索的类型(或者在NEW_TABLE 中包含TYPE)。

      insert into new_table (col1, sproc)
          select distinct tc.column_name, sp.name     
          from user_tab_columns tc
                  , user_source sp
                  , user_dependencies d
          where d.referenced_name = 'TABLE1'
          and   d.referenced_type = 'TABLE'
          and   d.type IN ('PACKAGE', 'PACKAGE BODY', 'FUNCTION'
                   , 'PROCEDURE',  'TYPE', 'TRIGGER')
          and   tc.table_name = 'TABLE1'
          and   sp.name = d.name
          and   instr(lower(sp.text), lower(tc.column_name)) > 0
      /
      

      【讨论】:

        猜你喜欢
        • 2011-10-15
        • 1970-01-01
        • 1970-01-01
        • 2012-01-01
        • 1970-01-01
        • 2022-01-06
        • 2011-11-09
        • 1970-01-01
        • 2017-01-07
        相关资源
        最近更新 更多