试图从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 上。