pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
How to pass table column values to function
3+ messages / 3 participants
[nested] [flat]

* How to pass table column values to function
@ 2023-02-11 10:10 Andrus <kobruleht2@hot.ee>
  2023-02-11 12:21 ` Re: How to pass table column values to function Marcos Pegoraro <marcos@f10.com.br>
  2023-02-11 14:16 ` Re: How to pass table column values to function jian he <jian.universality@gmail.com>
  0 siblings, 2 replies; 3+ messages in thread

From: Andrus @ 2023-02-11 10:10 UTC (permalink / raw)
  To: pgsql-general

Hi!

Table source contains integer column. Its values should be passed to 
function for selecting data from other table.

I tried

         CREATE OR REPLACE FUNCTION 
public.TestAddAssetTransactions(dokumnrs int[])
          RETURNS int AS
         $BODY$

         with i1 as (
         INSERT INTO bilkaib (dokumnr)
         select dokumnr  from dok where dokumnr in (select * from 
unnest(dokumnrs))
         returning *
         )

         select count(*) from i1;
         $BODY$ language sql;

         create temp table bilkaib (dokumnr int ) on commit drop;
         create temp table dok (dokumnr serial primary key ) on commit drop;
         create temp table source (dokumnr int ) on commit drop;
         insert into source values (1),(2);

     select TestAddAssetTransactions( (select ARRAY[dokumnr] from 
source)::int[] )


but got error

 > ERROR: more than one row returned by a subquery used as an expression

How to pass set of integers to function? Should temp table with fixed 
name used or is there better solution?

Using Postgresql 12+

Andrus.

^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: How to pass table column values to function
  2023-02-11 10:10 How to pass table column values to function Andrus <kobruleht2@hot.ee>
@ 2023-02-11 12:21 ` Marcos Pegoraro <marcos@f10.com.br>
  1 sibling, 0 replies; 3+ messages in thread

From: Marcos Pegoraro @ 2023-02-11 12:21 UTC (permalink / raw)
  To: Andrus <kobruleht2@hot.ee>; +Cc: pgsql-general

Em sáb., 11 de fev. de 2023 às 07:10, Andrus <kobruleht2@hot.ee> escreveu:

> Hi!
>
> Table source contains integer column. Its values should be passed to
> function for selecting data from other table.
>
> I tried
>
>         CREATE OR REPLACE FUNCTION
> public.TestAddAssetTransactions(dokumnrs int[])
>          RETURNS int AS
>         $BODY$
>
>         with i1 as (
>         INSERT INTO bilkaib (dokumnr)
>         select dokumnr  from dok where dokumnr in (select * from
> unnest(dokumnrs))
>         returning *
>         )
>
>         select count(*) from i1;
>         $BODY$ language sql;
>
>         create temp table bilkaib (dokumnr int ) on commit drop;
>         create temp table dok (dokumnr serial primary key ) on commit drop;
>         create temp table source (dokumnr int ) on commit drop;
>         insert into source values (1),(2);
>
>     select TestAddAssetTransactions( (select ARRAY[dokumnr] from
> source)::int[] )
>
>
> but got error
>
> > ERROR: more than one row returned by a subquery used as an expression
>
> Probably you want an array_agg and not an array
select TestAddAssetTransactions( (select array_agg(dokumnr) from source) )

^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: How to pass table column values to function
  2023-02-11 10:10 How to pass table column values to function Andrus <kobruleht2@hot.ee>
@ 2023-02-11 14:16 ` jian he <jian.universality@gmail.com>
  1 sibling, 0 replies; 3+ messages in thread

From: jian he @ 2023-02-11 14:16 UTC (permalink / raw)
  To: Andrus <kobruleht2@hot.ee>; +Cc: pgsql-general

On Sat, Feb 11, 2023 at 3:55 PM Andrus <kobruleht2@hot.ee> wrote:

> Hi!
>
> Table source contains integer column. Its values should be passed to
> function for selecting data from other table.
>
> I tried
>
>         CREATE OR REPLACE FUNCTION
> public.TestAddAssetTransactions(dokumnrs int[])
>          RETURNS int AS
>         $BODY$
>
>         with i1 as (
>         INSERT INTO bilkaib (dokumnr)
>         select dokumnr  from dok where dokumnr in (select * from
> unnest(dokumnrs))
>         returning *
>         )
>
>         select count(*) from i1;
>         $BODY$ language sql;
>
>         create temp table bilkaib (dokumnr int ) on commit drop;
>         create temp table dok (dokumnr serial primary key ) on commit drop;
>         create temp table source (dokumnr int ) on commit drop;
>         insert into source values (1),(2);
>
>     select TestAddAssetTransactions( (select ARRAY[dokumnr] from
> source)::int[] )
>
>
> but got error
>
> > ERROR: more than one row returned by a subquery used as an expression
>
> How to pass set of integers to function? Should temp table with fixed name
> used or is there better solution?
>
> Using Postgresql 12+
>
> Andrus.
>
try this:
select TestAddAssetTransactions((select array(select * from source)));

^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2023-02-11 14:16 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2023-02-11 10:10 How to pass table column values to function Andrus <kobruleht2@hot.ee>
2023-02-11 12:21 ` Marcos Pegoraro <marcos@f10.com.br>
2023-02-11 14:16 ` jian he <jian.universality@gmail.com>

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