Question

I have a function written in PL/pgSQL which is mentioned here:

CREATE OR REPLACE FUNCTION test()
RETURNS SETOF ccdb.consumers AS
$body$

BEGIN

RETURN QUERY
SELECT consumers.* FROM ccdb.consumers JOIN ccdb.consumer_index_details USING (cin) LIMIT 10;

END;
$body$
LANGUAGE plpgsql;

When I give consumers.* in the select statement then the function executes perfectly. But when I try to get the fields also from my consumer_index_details table then I get an error saying,

ERROR:  structure of query does not match function result type
DETAIL:  Returned type character varying(30) does not match expected type numeric(13,0) in column 2.
CONTEXT:  PL/pgSQL function test() line 5 at RETURN QUERY

Here what I understood by the error is that since in my return type I have given consumers table, it is expecting the output structure to be the same. So my question is, what should be the RETURN TYPE if I want to get the fields from multiple tables, the return could be dynamic?

Was it helpful?

Solution

You can try using RETURNS TABLE() with the list of columns as your table definition... Hope it works out for you..

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top