Oracle 数据库循环表

天才博士

我有一个 oracle 数据库,我正在运行以下查询

select table_name
from db_test.test
where table_name like '%20170128'

这会返回一列,其中包含所有带有特定日期的表。如何获取此列表并查询它们?

贾斯汀·凯夫

您需要动态 SQL。如果你对每个表运行一个简单的查询(我只是count(*)在这个例子中做一个),这样的事情会起作用

declare
  l_cnt integer;
  l_sql varchar2(1000);
begin 
  for t in (select table_name
              from db_test.test
             where table_name like '%20170128')
  loop
    l_sql := 'select count(*) from ' || t.table_name;
    execute immediate l_sql
                 into l_cnt;
    dbms_output.put_line( t.table_name || ' has ' || l_cnt || ' rows.' );
  end loop;
end;

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章