即使我有行,查询“select user_tables from num_rows”也不返回行数

时间:2014-06-03 05:37:57

标签: sql oracle11g

以下查询

select num_rows from user_tables; 
SQL中的

没有返回行数,显示为空如下,即使我在其中一个用户表中有两行。

SQL> select num_rows from user_tables;

  NUM_ROWS
----------

任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

尝试:

select COUNT(*) from user_tables;

OR

Select COUNT(table_name) from user_tables

num_rows返回no.of行值由DBMS_STATS更新。因此,它不包含表中的当前行数,但计算了上次运行DBMS_STATS时的近似值。


更新:用于计算数据库中所有表中的所有行:

1。更好的方式:

`select table_name, num_rows from user_tables;`

或者

2。使用功能(不推荐)

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

请注意这是给oracle的。对于Sql server请参考: How to fetch the row count for all tables in a SQL SERVER database