pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Adrian Klaver <adrian.klaver@aklaver.com>
To: Lorusso Domenico <domenico.l76@gmail.com>
To: pgsql-general@lists.postgresql.org
Subject: Re: Dynamic binding issue
Date: Mon, 12 Jun 2023 16:31:44 -0700
Message-ID: <6cef623d-782c-913b-7c9e-6e9be3b0e6d8@aklaver.com> (raw)
In-Reply-To: <CAJMpnG6sN3KWMq4KZ21eMUZWvGSpkYZ7S_XdBtLdb4_nFdksQQ@mail.gmail.com>
References: <CAJMpnG6sN3KWMq4KZ21eMUZWvGSpkYZ7S_XdBtLdb4_nFdksQQ@mail.gmail.com>

On 6/12/23 15:13, Lorusso Domenico wrote:
> Hello guys,
> I'm a problem with dynamic sql.
> I am trying to write a generic function that is able to read and update 
> a table based on some data coming from e previous record.
> Here the example
> _sqlStr=format('select *
>    from  %1$s.%2$s
>    where (' || array_to_string(_activeRec.pk_columns_list, ',') || ') in 
> (select ' ||
>    'row($1[''' || array_to_string(_activeRec.pk_columns_list, 
> '''],$1[''') || ''']))'
>    , _activeRec.name_of_schema, _activeRec.main_table);
> 
> execute _sqlStr using oldRec into _rec;
> 
> My problem is oldRec is a type record, so the substitution performed by 
> execute fails, because it can't recognize the field if the variable is 
> record and not a specific composite record type.

Why not use a row type?:

https://www.postgresql.org/docs/current/plpgsql-declarations.html#PLPGSQL-DECLARATION-ROWTYPES

> 
> I suppose this is a recurrent question, but I can't find a solution...
> 
> -- 
> Domenico L.

-- 
Adrian Klaver
adrian.klaver@aklaver.com






view thread (3+ messages)  latest in thread

Message-ID: <6cef623d-782c-913b-7c9e-6e9be3b0e6d8@aklaver.com>
Permalink:  ../6cef623d-782c-913b-7c9e-6e9be3b0e6d8@aklaver.com/
Also on:    postgresql.org/message-id/6cef623d-782c-913b-7c9e-6e9be3b0e6d8@aklaver.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: adrian.klaver@aklaver.com, domenico.l76@gmail.com
  Subject: Re: Dynamic binding issue
  In-Reply-To: <6cef623d-782c-913b-7c9e-6e9be3b0e6d8@aklaver.com>
  References: <CAJMpnG6sN3KWMq4KZ21eMUZWvGSpkYZ7S_XdBtLdb4_nFdksQQ@mail.gmail.com> <6cef623d-782c-913b-7c9e-6e9be3b0e6d8@aklaver.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox