2

I have worked out the following function

CREATE OR REPLACE FUNCTION lgis_overview(id_number int) /*id of object*/
        RETURNS TABLE(datatype text, object_number int, object_name text,output text, link text)
    AS
    $$
    BEGIN
        RETURN QUERY
    (select tags.datatype::text, objects.id::int, objects.name::text, lgis_getvalue(tags.datatype::text, foo.id::int)::text, lgis_objectname(output)
    from tags, objects,(select * from values
    where object_id = id_number) as foo

    where tags.id = foo.tag_id
    and objects.id = foo.object_id);
   
END;
    $$
LANGUAGE 'plpgsql' VOLATILE;

This function gives this output:

select * from lgis_overview(148830)

enter image description here

Unfortunatelly, when i use this function in this form, the output is a list of record types:

select lgis_overview(id)
from objects 
limit 10

enter image description here

In what (clean) way can I rewrite the function so that the output is a table, or how should I use the function so that the output is a table?

1
  • As a side note: this doesn't require PL/pgSQL and the language name is an identifier. It should not be enclosed in single quotes. That syntax is deprecated and support for it might be removed in a future version. Better use language plpgsql. Commented May 20, 2021 at 5:47

1 Answer 1

3

Set returning functions should be used in the FROM clause (as you did in your first query).

If you need to pass it a column value, use a lateral join:

select ov.*
from objects as o
  cross join lateral lgis_overview(o.id) as ov
limit 10
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.