小编Tot*_*tor的帖子

在Postgres 9.0+中使用PL/pgSQL循环表

我想遍历所有表来计算每个表中的行数.以下查询给我一个错误:

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$$;
Run Code Online (Sandbox Code Playgroud)

错误:

ERROR:  syntax error at or near ")"
LINE 1: SELECT count(*) FROM (sql_features)
                                          ^
QUERY:  SELECT count(*) FROM (sql_features)
CONTEXT:  PL/pgSQL function inline_code_block line 8 at EXECUTE statement
Run Code Online (Sandbox Code Playgroud)

sql_features是我的数据库中的表名.我已经尝试使用quote_ident()但无济于事.

postgresql variables loops plpgsql tablename

17
推荐指数
2
解决办法
6万
查看次数

标签 统计

loops ×1

plpgsql ×1

postgresql ×1

tablename ×1

variables ×1