Loop on tables with PL/pgSQL in Postgres 9.0+
问题 I want to loop through all my tables to count rows in each of them. The following query gets me an error: DO $$ DECLARE tables CURSOR FOR SELECT tablename FROM pg_tables WHERE tablename NOT LIKE 'pg_%' ORDER BY tablename; tablename varchar(100); nbRow int; BEGIN FOR tablename IN tables LOOP EXECUTE 'SELECT count(*) FROM ' || tablename INTO nbRow; -- Do something with nbRow END LOOP; END$$; Errors: ERROR: syntax error at or near ")" LINE 1: SELECT count(*) FROM (sql_features) ^ QUERY: SELECT