Skip to content
Advertisement

H2 Get row count for all tables

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).

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement