sql - Oracle 模式表行数

标签 sql linux oracle11g count row

甲骨文:11g 操作系统:Linux

我有一个非常棘手的问题,我正在尝试解决,但无法得到明确的答案...... 我确实在谷歌等上进行了搜索,但没有满足我的要求...

架构统计信息不可靠,因此想要查询 dba_tables.. 也不想在数据库下创建任何过程或函数..只是想用简单的SQL来实现。

问。 如何假脱机特定模式的所有表行数并显示 table_name?

A. 我可以在假脱机中轻松显示计数,但无法获取计数旁边的表名称..

例如

Table_Name Count
tab1 200
tab2 500
tab3 300

下面我可以得到计数,但无法弄清楚结果中显示的 table_name...

spool runme.sql

select 'select count(*) from '|| owner || '.' || table_name || ';' 
from dba_tables
where owner = 'user1'
order by table_name;

spool off

最佳答案

你可以使用这样的函数,但它会很慢:

create or replace
function get_rows( p_tname in varchar2 ) return number
as
    l_columnValue    number default NULL;
begin
    execute immediate
       'select count(*)
          from ' || p_tname INTO l_columnValue;

    return l_columnValue;
end;
/

select user, table_name,
       get_rows( user||'.'||table_name) cnt
  from user_tables
/

代码取自 Tom Kyte 网站上的答案:

http://asktom.oracle.com/pls/asktom/f?p=100:11:::::P11_QUESTION_ID:1660875645686

没有函数调用也可以:

select
table_name,
to_number(
   extractvalue(
      xmltype(
         dbms_xmlgen.getxml('select count(*) c from '||table_name))
,'/ROWSET/ROW/C')) count
from user_tables;

来自此处的提示:

http://laurentschneider.com/wordpress/2007/04/how-do-i-store-the-counts-of-all-tables.html

关于sql - Oracle 模式表行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16942427/

相关文章:

sql - 如何从数据库 grails 重新加载域类的实例?

sql - postgres 使用副本插入带有字段时间戳的 CSV 时出错

mysql - SQL命令根据插入的数据自动创建表

Linux从文件中取出字符串并将其用作文件名?

sql - CTAS 功能

oracle - for 循环中的引用游标

sql - 如何在 ActiveRecord 中为 SUM 添加 where 作用域?

python - 如何让python程序在终端输入命令并从终端获取输出

python - 以 subprocess 模块启动的进程进入休眠状态

oracle - PL/SQL ORA-01422 : exact fetch returns more than requested number of rows