2

试图从PLV8存储过程返回表。

它无法识别输入参数,即$1当函数与 RETURNS TABLE(...).

它在函数返回标量时起作用:

psql# CREATE OR REPLACE function foo(integer) 
      RETURNS integer 
      LANGUAGE plv8 
      AS $$
         var a=$1;
         return a;
      $$;
CREATE FUNCTION
psql# SELECT * FROM foo(10);
 foo 
-----
  10
(1 row)

它也适用于 function RETURNS SETOF

案例1)具有自定义数据类型:

psql# CREATE TYPE myrow as (bar int);
CREATE TYPE
psql# CREATE OR REPLACE function foo(integer) 
      RETURNS SETOF myrow
      LANGUAGE plv8 
      AS $$
         var a=$1;
         return {"bar": a};
         // Or alternatively:
         // return plv8.execute('SELECT ' + a +' AS bar');
      $$;
CREATE FUNCTION
psql# SELECT * FROM foo(10);
 bar 
-----
  10
(1 row)

案例2)与SETOF record

psql# CREATE OR REPLACE function foo(integer) 
      RETURNS SETOF record
      LANGUAGE plv8 
      AS $$
         var a=$1;
         return {"bar": a};
         // Or alternatively:
         // return plv8.execute('SELECT ' + a +' AS bar');
      $$;
CREATE FUNCTION
psql# SELECT * FROM foo(10) AS xxx(bar int);
 bar 
-----
  10
(1 row)

但它似乎不适用于RETURNS TABLE

psql# CREATE OR REPLACE FUNCTION foo(integer) 
      RETURNS TABLE(bar int)  
      LANGUAGE plv8 
      AS $$
         var a=$1;
         return {"bar": a};
         // Doesn't matter cause it doesn't make it here, 
         // but alternative 'return' also fails with same error:
         // return plv8.execute('SELECT ' + a + ' AS bar');
      $$;
CREATE FUNCTION

psql# SELECT * FROM foo(10);
ERROR:  ReferenceError: $1 is not defined
DETAIL:  foo() LINE 2: var a=$1;

知道为什么会窒息$1吗?

我在 Postgres 9.4.0 和 Plv8 1.4.4 上。

4

1 回答 1

1

使用命名参数似乎可以正常工作,例如“CREATE OR REPLACE FUNCTION foo(a integer)”

我实际上是在寻找如何从 plv8 函数返回多个值。为了将来参考,OUT 参数似乎是要走的路,例如

CREATE OR REPLACE FUNCTION foo(a integer, OUT b integer, OUT c integer) 
RETURNS setof record
LANGUAGE plv8 
AS $$

return {b: a * 2, c: a * 10};

$$;
于 2015-08-10T20:22:59.323 回答