I am using PostgreSQL 11 version.
I want to implement a function that takes the layer name (table), column name, and id as parameters.
create or replace function test(layer_name anyelement, field_name anyelement, object_id text)
returns setof anyelement
language plpgsql
as $function$
begin
return query execute format('
select
*
from
%s
where
%s = cast($1 as int4)'
, pg_typeof(layer_name), pg_typeof(field_name))
using object_id;
end;
$function$
;
This is the code I've implemented and when I call the function I get an error.
What am I doing wrong?
Error querying database. Cause: org.postgresql.util.PSQLException: ERROR: syntax error at or near "="
Where: PL/pgSQL function test(anyelement,anyelement,text) line 3 at RETURN QUERY
### The error occurred while setting parameters
### SQL: select * from test(?, ?, ?)
### Cause: org.postgresql.util.PSQLException: ERROR: syntax error at or near "="
Where: PL/pgSQL function test(anyelement,anyelement,text) line 3 at RETURN QUERY
; bad SQL grammar []; nested exception is org.postgresql.util.PSQLException: ERROR: syntax error at or near "="
Where: PL/pgSQL function test(anyelement,anyelement,text) line 3 at RETURN QUERY}
org.springframework.jdbc.BadSqlGrammarException:
### Error querying database. Cause: org.postgresql.util.PSQLException: ERROR: syntax error at or near "="
Where: PL/pgSQL function test(anyelement,anyelement,text) line 3 at RETURN QUERY
### The error occurred while setting parameters
### SQL: select * from test(?, ?, ?)
### Cause: org.postgresql.util.PSQLException: ERROR: syntax error at or near "="
Where: PL/pgSQL function test(anyelement,anyelement,text) line 3 at RETURN QUERY
; bad SQL grammar []; nested exception is org.postgresql.util.PSQLException: ERROR: syntax error at or near "="
Where: PL/pgSQL function test(anyelement,anyelement,text) line 3 at RETURN QUERY
layerand why you are using pg_typeof(layer_name)?