在无法接受集合的上下文中调用的集合值函数
set-valued function called in context that cannot accept a set
我收到错误消息:
set-valued function called in context that cannot accept a set
在 RETURN QUERY EXECUTE
行执行此函数时:
PLSQL $ cat lookup_email.pl
CREATE OR REPLACE FUNCTION app.lookup_email(ident_id bigint,sess bigint,company_id bigint,email varchar)
RETURNS SETOF RECORD as $$
DECLARE
rec RECORD;
comp_id bigint;
server_session bigint;
schema_name varchar;
query varchar;
BEGIN
schema_name:='comp' || company_id;
select app.session.session into server_session from app.session where app.session.identity_id=ident_id and app.session.session=sess;
IF FOUND
THEN
BEGIN
query:='SELECT i.email,u.user_id FROM app.identity as i,' || schema_name || '.uzer as u WHERE i.email like ''%' || email || '%'' and i.identity_id=u.identity_id';
RAISE NOTICE 'executing: %',query;
RETURN QUERY EXECUTE query;
RETURN;
EXCEPTION
WHEN OTHERS THEN
RAISE NOTICE ' query error (%)',SQLERRM;
END;
END IF;
END;
$$ LANGUAGE plpgsql;
这是 psql 的输出:
dev=> select app.lookup_email(4,730035455897450,6,'u');
NOTICE: executing: SELECT i.email,u.user_id FROM app.identity as i,comp6.uzer as u WHERE i.email like '%u%' and i.identity_id=u.identity_id
NOTICE: query error (set-valued function called in context that cannot accept a set)
lookup_email
--------------
(0 rows)
我知道查询不包含任何错误,因为它在另一个 psql 会话中工作:
dev=> SELECT i.email,u.user_id FROM app.identity as i,comp6.uzer as u WHERE i.email like '%u%' and i.identity_id=u.identity_id;
email | user_id
----------------+---------
hola@mundo.com | 1
(1 row)
那么,如果我将我的函数声明为 RETURNS SETOF RECORD
,为什么 Postgres 会抱怨?我的错误在哪里?
So, why is Postgres complaining if I declared my function being a SET of RECORD ??? Where is my error?
- 在 FROM 子句中调用您的 Set 返回函数。
- 始终指定您的类型。
它被称为集合返回函数,但您想指定复合类型
这完全正确,
RETURNS SETOF RECORD $$
但是,您可能需要调用它,
SELECT email, user_id
FROM
app.lookup_email(4,730035455897450,6,'u')
AS t(email text, user_id integer)
不能在其中调用非类型化 SRF 的上下文是没有 table 定义的上下文。这种语法可能会变得令人讨厌,所以将 RETURNS SETOF RECORD
更改为
更容易
RETURNS TABLE(email text, user_id integer) AS $$
并使用没有列定义列表的函数
SELECT email, user_id
FROM app.lookup_email(4,730035455897450,6,'u')
在 the docs
中查找更多信息
在函数中定义输出列名
喜欢下面
CREATE OR REPLACE FUNCTION app.lookup_email(ident_id bigint,sess bigint,company_id bigint,email varchar, OUT <column_name> <data type>, OUT ...)
我收到错误消息:
set-valued function called in context that cannot accept a set
在 RETURN QUERY EXECUTE
行执行此函数时:
PLSQL $ cat lookup_email.pl
CREATE OR REPLACE FUNCTION app.lookup_email(ident_id bigint,sess bigint,company_id bigint,email varchar)
RETURNS SETOF RECORD as $$
DECLARE
rec RECORD;
comp_id bigint;
server_session bigint;
schema_name varchar;
query varchar;
BEGIN
schema_name:='comp' || company_id;
select app.session.session into server_session from app.session where app.session.identity_id=ident_id and app.session.session=sess;
IF FOUND
THEN
BEGIN
query:='SELECT i.email,u.user_id FROM app.identity as i,' || schema_name || '.uzer as u WHERE i.email like ''%' || email || '%'' and i.identity_id=u.identity_id';
RAISE NOTICE 'executing: %',query;
RETURN QUERY EXECUTE query;
RETURN;
EXCEPTION
WHEN OTHERS THEN
RAISE NOTICE ' query error (%)',SQLERRM;
END;
END IF;
END;
$$ LANGUAGE plpgsql;
这是 psql 的输出:
dev=> select app.lookup_email(4,730035455897450,6,'u');
NOTICE: executing: SELECT i.email,u.user_id FROM app.identity as i,comp6.uzer as u WHERE i.email like '%u%' and i.identity_id=u.identity_id
NOTICE: query error (set-valued function called in context that cannot accept a set)
lookup_email
--------------
(0 rows)
我知道查询不包含任何错误,因为它在另一个 psql 会话中工作:
dev=> SELECT i.email,u.user_id FROM app.identity as i,comp6.uzer as u WHERE i.email like '%u%' and i.identity_id=u.identity_id;
email | user_id
----------------+---------
hola@mundo.com | 1
(1 row)
那么,如果我将我的函数声明为 RETURNS SETOF RECORD
,为什么 Postgres 会抱怨?我的错误在哪里?
So, why is Postgres complaining if I declared my function being a SET of RECORD ??? Where is my error?
- 在 FROM 子句中调用您的 Set 返回函数。
- 始终指定您的类型。
它被称为集合返回函数,但您想指定复合类型
这完全正确,
RETURNS SETOF RECORD $$
但是,您可能需要调用它,
SELECT email, user_id
FROM
app.lookup_email(4,730035455897450,6,'u')
AS t(email text, user_id integer)
不能在其中调用非类型化 SRF 的上下文是没有 table 定义的上下文。这种语法可能会变得令人讨厌,所以将 RETURNS SETOF RECORD
更改为
RETURNS TABLE(email text, user_id integer) AS $$
并使用没有列定义列表的函数
SELECT email, user_id
FROM app.lookup_email(4,730035455897450,6,'u')
在 the docs
中查找更多信息在函数中定义输出列名 喜欢下面
CREATE OR REPLACE FUNCTION app.lookup_email(ident_id bigint,sess bigint,company_id bigint,email varchar, OUT <column_name> <data type>, OUT ...)