I have seen a similar post here but my situation is slightly different from anything I've found so far. I am trying to call a postgres function with parameters that I can leverage in the function logic as they pertain to the jsonb query. Here is an example of the query I'm trying to recreate with parameters.
SELECT *
from edit_data
where ( "json_field"#>'{Attributes}' )::jsonb @>
'{"issue_description":"**my description**",
"reporter_email":"**[email protected]**"}'::jsonb
I can run this query just fine in PGAdmin but all my attempts thus far to run this inside a function with parameters for "my description" and "[email protected]" values have failed. Here is a simple example of the function I'm trying to create:
CREATE OR REPLACE FUNCTION get_Features(
p1 character varying,
p2 character varying)
RETURNS SETOF edit_metadata AS
$BODY$
SELECT * from edit_metadata where ("geo_json"#>'{Attributes}' )::jsonb @> '{"issue_description":**$p1**, "reporter_email":**$p2**}'::jsonb;
$BODY$
LANGUAGE sql VOLATILE
COST 100
ROWS 1000;
I know that the syntax is incorrect and I've been struggling with this for a day or two. Can anyone help me understand how to best deal with these double quotes around the value and leverage a parameter here?
TIA
'{"issue_description":"'||yourParam||'"
and so on. – Jorge Campos Mar 2 at 18:00'{"issue_description":"**my description**", "reporter_email":"**[email protected]**"}'
is a String and as so you cant use funcion, command or variables inside it and hope postgresql will understand, it is not PHP, you have to say to it that you are forming a NEW string. It should be like (for your case):'{"issue_description":"'||p1||'", "reporter_email":"'||p2||'"}'
That way you are telling to postgresql that you want to "inject" your parameter inside your String – Jorge Campos Mar 2 at 18:14