---
title: "[转]Oracle查询数据库中所有表"
author: "wjeye"
date: "2011-06-24T13:59:56Z"
tags: []
categories: ["数据库"]
slug: "oracle"
draft: false
lastmod: "2011-06-24T14:00:25Z"
id: 1103659
url: "https://wjeye.iteye.com/blog/1103659"
---

### Oracle查询数据库中所有表的记录数\[转]

 

select t.table\_name,t.num\_rows from user\_tables t

若以上SQL查找不到（反正我的是查不到），执行如下脚本即可：  
&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;  
create or replace function count\_rows(table\_name in varchar2,  
                              owner in varchar2 default null)  
return number  
authid current\_user  
IS  
   num\_rows number;  
   stmt varchar2(2000);  
begin  
   if owner is null then  
      stmt := 'select count(\*) from "'||table\_name||'"';  
   else  
      stmt := 'select count(\*) from "'||owner||'"."'||table\_name||'"';  
   end if;  
   execute immediate stmt into num\_rows;  
   return num\_rows;  
end;  
&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;

之后，再查select table\_name, count\_rows(table\_name) nrows from user\_tables,就可以了

 

 

 2、查询所有用户的 数据库表记录

select t.owner, count(\*) from all\_tables t group by t.owner