简体   繁体   中英

PostgreSQL - How to create an integer array from a select to pass as an argument to a function

I have a function that receives an integer[] array as a parameter.

CREATE OR REPLACE FUNCTION public.get_master_products(P_PRODUCTS_IDS integer[])
  RETURNS SETOF record
  LANGUAGE plpgsql
AS
$body$
DECLARE
    CURRENT_ID integer;
    PRODUCT RECORD;
    COMPONENT1 RECORD;
    COMPONENT2 RECORD;
    COMPONENT3 RECORD;
BEGIN
....

END;

And when I call this function I have to do it like this:

SELECT
    "ID",
    "CODE",
    "CODE2",
    "NAME",
    "COMPOSERS",
    "PTYPE_ID",
    "PTYPE_NAME",
    "COMPONENT_QUANTITY",
    "UM_ID",
    "LEVEL"
FROM 
    get_master_products(array[ 20, 40, 50, 80])
AS
    (
      "ID" integer,
      "CODE" varchar,
      "CODE2" varchar,
      "NAME" varchar,
      "COMPOSERS" numeric,
      "PTYPE_ID" bigint,
      "PTYPE_NAME" varchar,
      "COMPONENT_QUANTITY" numeric,
      "UM_ID" varchar,
      "LEVEL" varchar
    );

but is there a way to do something like this to call the function?:

get_master_products(array[ (SELECT id, FROM products WHERE composers > 0) ])

to automatically create the pass array with a SQL query??

Can you help me please.

Thank you

使用array_agg

get_master_products(SELECT array_agg(id) FROM products WHERE composers > 0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM