---
title: "Oracle查询每张表的记录数"
author: "wmlJava"
date: "2015-07-26T12:49:32Z"
tags: []
categories: ["数据库"]
slug: "oracle"
draft: false
lastmod: "2015-07-26T12:49:32Z"
id: 2230308
url: "https://wmljava.iteye.com/blog/2230308"
---

```
--1。执行
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;
 
--2.执行
select table_name, count_rows(table_name) nrows
  from user_tables
 order by table_name asc
```