From: | Wells Oliver <wells(dot)oliver(at)gmail(dot)com> |
---|---|
To: | Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> |
Cc: | pgsql-admin <pgsql-admin(at)postgresql(dot)org> |
Subject: | Re: plpgsql, dynamic variable lengths.. |
Date: | 2020-02-08 02:23:31 |
Message-ID: | CAOC+FBVj-CuZriMmLa0oEWONuQQmzzLN9Bzu8cF6AGykh+OUmA@mail.gmail.com |
Views: | Whole Thread | Raw Message | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-admin |
Yes, but the issue is it won't always be three params, and not always the
same variable in $1, $2, etc. It depends on which keys are present in the
passed JSON object. Which I why I think I need to do something else like
create a recordset and pass that in USING, or something. Just can't quite
figure it out...
On Fri, Feb 7, 2020 at 6:09 PM Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
> Wells Oliver <wells(dot)oliver(at)gmail(dot)com> writes:
> > I am trying to create a plpgsql function which accepts a jsonb object as
> > its parameter.
>
> > This jsonb can have up to three keys, and I am trying to build out the
> > dynamic SQL like this. I want to be able to pass a variable number of
> > params to the RETURN QUERY EXECUTE ... USING ... because it can be 0
> params
> > in the JSONB object, or one, or two.
>
> > What's the missing piece here? How can I adjust my USING to accomplish
> this?
>
> I think your USING is probably fine. But you made all the SQL fragments
> refer to $1:
>
> > begin
> > SQLSTRING := 'SELECT * FROM mytable WHERE true';
> > if args->>'col1' is not null then
> > SQLSTRING := SQLSTRING || ' AND col1 = $1';
> > end if;
>
> > if args->>'col2' is not null then
> > SQLSTRING := SQLSTRING || ' AND col2 = $1';
> > end if;
>
> > if args->>'col3' is not null then
> > SQLSTRING := SQLSTRING || ' AND col3 = $1';
> > end if;
>
> Of course those need to be $1, $2, $3.
>
> regards, tom lane
>
--
Wells Oliver
wells(dot)oliver(at)gmail(dot)com <wellsoliver(at)gmail(dot)com>
From | Date | Subject | |
---|---|---|---|
Next Message | Tom Lane | 2020-02-08 02:36:03 | Re: plpgsql, dynamic variable lengths.. |
Previous Message | Tom Lane | 2020-02-08 02:09:44 | Re: plpgsql, dynamic variable lengths.. |