甲骨文: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/