set-valued function called in context that cannot accept a set

十年热恋 提交于 2019-12-04 09:47:25
Evan Carroll

So, why is Postgres complaining if I declared my function being a SET of RECORD ??? Where is my error?

  1. Call your Set Returning Function in a FROM clause.
  2. Always specify your types.

It's called a Set Returning Function, but you want to specify the composite type

This is totally valid,

RETURNS SETOF RECORD $$

However, you may have to call it with,

SELECT email, user_id
FROM 
    app.lookup_email(4,730035455897450,6,'u')
    AS t(email text, user_id integer)

The context which you can not call an untyped SRF in, is one which does not have a table-definition. This syntax can get nasty, so just it's easier to change RETURNS SETOF RECORD to

RETURNS TABLE(email text, user_id integer) AS $$

and use the function without the column definition list

SELECT email, user_id
FROM app.lookup_email(4,730035455897450,6,'u')

Find more information in the docs

defind output column name in function like down below

CREATE OR REPLACE FUNCTION app.lookup_email(ident_id bigint,sess bigint,company_id bigint,email varchar, OUT <column_name> <data type>, OUT ...)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!