Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

H2 Get row count for all tables

Tags:

sql

count

h2

I would like to know the total number of rows for each table. And that for all tables at once. Is this possible with H2?

I'm thinking of a table:

|table name | row size |

At the moment I'm using the current version 1.3.170 (2012-11-30).

like image 841
keiki Avatar asked Feb 04 '13 09:02

keiki


1 Answers

You would need to use a user-defined function, for example:

drop alias count_rows;
create alias count_rows as 
'long countRows(Connection conn, String tableName) 
    throws SQLException {
ResultSet rs = conn.createStatement().
    executeQuery("select count(*) from " + tableName);
rs.next();
return rs.getLong(1); }';
select table_name, count_rows(table_name) count 
from INFORMATION_SCHEMA.TABLES 
where table_schema = 'PUBLIC';

This code doesn't take into account the schema, and only works for unquoted table names.

like image 167
Thomas Mueller Avatar answered Sep 24 '22 16:09

Thomas Mueller