【问题标题】:How to properly parse an iterator in postgresql?如何正确解析 postgresql 中的迭代器?
【发布时间】:2016-10-15 19:58:48
【问题描述】:

我有一个巨大的 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$

包含冒号的列名会导致以下错误:

错误:“:”处或附近的语法错误
第 1 行:ALTER TABLE rail_l DROP COLUMN 生成器:来源

查询:ALTER TABLE rail_l DROP COLUMN 生成器:来源
CONTEXT:PL/pgSQL 函数 inline_code_block 第 16 行在 EXECUTE 语句
SQL 状态:42601

也许是一个初学者的问题,因为到目前为止我只将 postgresql 用于简单查询,但谢天谢地,我会接受任何建议。

【问题讨论】:

    标签: postgresql parsing iterator openstreetmap isnull


    【解决方案1】:

    format()与说明符%I一起使用:

    ...
    LOOP
       RAISE NOTICE '%',  _column;
       EXECUTE format('ALTER TABLE rail_l DROP COLUMN %I', _column);
    END LOOP;
    ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-14
      • 1970-01-01
      • 1970-01-01
      • 2019-11-02
      • 2015-09-05
      相关资源
      最近更新 更多