Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pQmq9-0004nG-DP for pgsql-general@arkaria.postgresql.org; Sat, 11 Feb 2023 10:10:41 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pQmq8-0006i9-A4 for pgsql-general@arkaria.postgresql.org; Sat, 11 Feb 2023 10:10:40 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pQmq7-0006hz-V3 for pgsql-general@lists.postgresql.org; Sat, 11 Feb 2023 10:10:39 +0000 Received: from portal-smtp-out.hot.ee ([194.126.101.125]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pQmq4-0000pZ-JG for pgsql-general@postgresql.org; Sat, 11 Feb 2023 10:10:38 +0000 Received: from localhost (localhost [127.0.0.1]) by vm-hot-relay10.estpak.ee (Postfix) with ESMTP id D606B24E1 for ; Sat, 11 Feb 2023 12:10:31 +0200 (EET) X-Virus-Scanned: Debian amavisd-new at vm-hot-relay10.estpak.ee Received: from portal-smtp-out.hot.ee ([127.0.0.1]) by localhost (vm-hot-relay10.estpak.ee [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id K39L2WjdXj6Y for ; Sat, 11 Feb 2023 12:10:29 +0200 (EET) Received: from mail.hot.ee (hot-relayhost1.estpak.ee [213.180.2.79]) by vm-hot-relay10.estpak.ee (Postfix) with ESMTP id 98528253D for ; Sat, 11 Feb 2023 12:10:29 +0200 (EET) Content-Type: multipart/alternative; boundary="------------th7Rt90AiEQ1oXMnXR1rXtId" Message-ID: <438cee94-b9f2-6626-e38a-dd33831af718@hot.ee> Date: Sat, 11 Feb 2023 12:10:25 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Thunderbird/102.7.2 Content-Language: et To: pgsql-general From: Andrus Subject: How to pass table column values to function List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------th7Rt90AiEQ1oXMnXR1rXtId Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit 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. --------------th7Rt90AiEQ1oXMnXR1rXtId Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit

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.

--------------th7Rt90AiEQ1oXMnXR1rXtId--