Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

postgresql, pgadmin error RETURN cannot have a parameter in function returning set

I made a function like this

create or replace function get_source2(TableName character varying)
returns setof integer as
$$
declare
    geom geometry;
    snode integer;
begin
    execute ('select get_source(geom) from '|| TableName)
        into snode;
    return snode;
end
$$
language plpgsql;

output data would be array type

so I try to use setof.

If I don't use setof, it runs well but only one result comes out.

However if I use setof I get an error like that

error : RETURN cannot have a parameter in function returning set
like image 719
andrew Avatar asked Nov 29 '16 11:11

andrew


1 Answers

Be careful! The way your function is written it is vulnerable to SQL injection!

You should use

$$BEGIN
   RETURN QUERY EXECUTE 'SELECT get_source(geom) FROM '
                        || quote_ident(TableName);
END;$$

Then RETURNS SETOF integer will work well.

Read up in the documentation how to use RETURN in a table function:

Either you do

RETURN NEXT expression1;
RETURN NEXT expression2;
...
RETURN;

or

RETURN QUERY SELECT ...;

or

RETURN QUERY EXECUTE 'SELECT ...';

but never

RETURN expression;
like image 85
Laurenz Albe Avatar answered Nov 03 '22 01:11

Laurenz Albe