如何正确解析 postgresql 中的迭代器?
How to properly parse an iterator in postgresql?
我有一个巨大的 OSM 数据集,其中包含许多我想删除的纯空列。
DO
$do$
DECLARE
_column TEXT;
BEGIN
FOR _column IN
SELECT attname
FROM pg_stats where tablename = 'rail_l'
and most_common_vals is null
and most_common_freqs is null
and histogram_bounds is null
and correlation is null
and null_frac = 1
LOOP
RAISE NOTICE '%', _column;
EXECUTE
'ALTER TABLE rail_l DROP COLUMN ' || _column;
END LOOP;
END
$do$
包含冒号的列名导致以下错误:
ERROR: syntax error at or near ":"
LINE 1: ALTER TABLE rail_l DROP COLUMN generator:source
QUERY: ALTER TABLE rail_l DROP COLUMN generator:source
CONTEXT: PL/pgSQL function inline_code_block line 16 at EXECUTE statement
SQL state: 42601
也许是初学者的问题,因为到目前为止我只将 postgresql 用于简单的查询,但我很乐意接受任何建议。
使用 format()
和说明符 %I
:
...
LOOP
RAISE NOTICE '%', _column;
EXECUTE format('ALTER TABLE rail_l DROP COLUMN %I', _column);
END LOOP;
...
我有一个巨大的 OSM 数据集,其中包含许多我想删除的纯空列。
DO
$do$
DECLARE
_column TEXT;
BEGIN
FOR _column IN
SELECT attname
FROM pg_stats where tablename = 'rail_l'
and most_common_vals is null
and most_common_freqs is null
and histogram_bounds is null
and correlation is null
and null_frac = 1
LOOP
RAISE NOTICE '%', _column;
EXECUTE
'ALTER TABLE rail_l DROP COLUMN ' || _column;
END LOOP;
END
$do$
包含冒号的列名导致以下错误:
ERROR: syntax error at or near ":"
LINE 1: ALTER TABLE rail_l DROP COLUMN generator:source
QUERY: ALTER TABLE rail_l DROP COLUMN generator:source
CONTEXT: PL/pgSQL function inline_code_block line 16 at EXECUTE statement
SQL state: 42601
也许是初学者的问题,因为到目前为止我只将 postgresql 用于简单的查询,但我很乐意接受任何建议。
使用 format()
和说明符 %I
:
...
LOOP
RAISE NOTICE '%', _column;
EXECUTE format('ALTER TABLE rail_l DROP COLUMN %I', _column);
END LOOP;
...