I am receiving the error:
set-valued function called in context that cannot accept a set
when executing this function at RETURN QUERY EXECUTE
line:
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;
This is the ouput from 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)
I know the query doesn't contain any error, because it works in another psql session:
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
----------------+---------
[email protected] | 1
(1 row)
So why is Postgres complaining if I declared my function being as RETURNS SETOF RECORD
? Where is my error?
SELECT version()
) – Sclerotic