PostgreSQL 9.0的功能问题

时间:2014-10-23 15:22:04

标签: postgresql

这是我正在使用的功能:

CREATE OR REPLACE FUNCTION f_multiply_row(_tbl regclass
                                        , _idname text
                                        , _minid int
                                        , _maxid int)
  RETURNS void AS
$func$
BEGIN

   EXECUTE (
      SELECT format('INSERT INTO %1$s (%2$I, %3$s)
                     SELECT g.g, %3$s
                     FROM  (SELECT * FROM %1$s LIMIT 1) t
                           ,generate_series($1, $2) g(g)'::text
                   , _tbl
                   , _idname
                   , string_agg(quote_ident(attname), ', ')
                   )
      FROM   pg_attribute 
      WHERE  attrelid = _tbl
      AND    attname <> _idname  -- exclude id column
      AND    NOT attisdropped    -- no dropped (dead) columns
      AND    attnum > 0          -- no system columns
      )
   USING _minid, _maxid;

END
$func$ LANGUAGE plpgsql;

SELECT f_multiply_row('campaign', 'campaign_id', 50, 500);

显然PSQL 9.0没有format方法(在9.1中引入),所以我试图将方法转换为9.0中的 work ,并且最难将它转换为工作

Error : ERROR: function format(text, regclass, text, text) does not exist LINE 2: SELECT format('INSERT INTO %1$s (%2$I, %3$s) ^ HINT: No function matches the given name and argument types. You might need to add explicit type casts.

所以,我试图用9.0风格重写它:

CREATE OR REPLACE FUNCTION f_multiply_row()
  RETURNS void AS
$func$
DECLARE
    _tbl regclass := quote_ident('campaign');
    _idname text := 'campaign_id';
    _minid int := 50;
    _maxid int := 500;

BEGIN

   EXECUTE (
      'SELECT INSERT INTO ' || _tbl || ' ($1, $2)
              SELECT g.g $3 FROM (SELECT * FROM ' || _tbl || ' LIMIT 1) t, generate_series($1, $2) g(g)'
        USING _tbl, _idname, string_agg(quote_ident(attname, ', '))
      FROM   pg_attribute 
      WHERE  attrelid = _tbl
      AND    attname <> _idname  -- exclude id column
      AND    NOT attisdropped    -- no dropped (dead) columns
      AND    attnum > 0          -- no system columns
    );

END
$func$ LANGUAGE plpgsql;

SELECT f_multiply_row();

但是,上述原因导致了这两个错误:

Error : ERROR: syntax error at or near "USING" LINE 15: USING _tbl, _idname, string_agg(quote_ident(attname, ', ')... ^

Error : ERROR: function f_multiply_row() does not exist LINE 1: SELECT f_multiply_row() ^ HINT: No function matches the given name and argument types. You might need to add explicit type casts.

0 个答案:

没有答案