PostgreSQL:如何将一个函数传递给数组并在一个查询中使用它与IN运算符

时间:2016-02-19 18:02:10

标签: sql postgresql

我有一个问题,我想将一个数组传递给postgres函数并使用该数组,因此返回SELECT IN子句中的值。

但它向我显示了这个错误:

An error occurred executing the SQL command :
SELECT
      *
  FROM
      get_invtransferences_porders_fporders (300001300 , array [ 300093753 , 300094126 , 300093349 , 300093838 , 300094128 ] ...

ERROR: operator does not exist : integer = integer [ ]
  Hint : No operator matches the name and type of arguments. You may need to add explicit type conversions .
  Where : PL / pgSQL get_invtransferences_porders_fporders (numeric , integer []) function on line 8 FOR loop around rows of a SELECT

这是我的功能:

CREATE OR REPLACE FUNCTION public.get_invtransferences_porders_fporders(p_product_id numeric, P_INVTRANSFERENCES_IDS integer[])
  RETURNS SETOF record
  LANGUAGE plpgsql
AS
$body$
DECLARE
    PORDER_PRODUCT RECORD;
    COMPONENT RECORD;
    COMPONENT2 RECORD;
    COMPONENT3 RECORD;
BEGIN
    FOR PORDER_PRODUCT IN (
                      SELECT 
                        'porder'                                                    AS "operation_type"
                        ,porders.id                                                 AS "porder_id"
                        ,porders.user_id                                            AS "porder_user_id"
                        ,(SELECT name FROM users WHERE users.id = porders.user_id)  AS "porder_user_name"
                        ,porders.delivery_datetime                                  AS "porder_delivery_datetime"
                        ,porders_products.requested                                 AS "product_requested"
                        ,porders_products.produced                                  AS "product_produced"
                        ,products.code                                              AS "product_code"
                        ,products.NAME                                              AS "product_name"
                        ,(
                          SELECT products.name
                          FROM products
                          WHERE id = product_components.component_product_id
                          )                                                         AS "component_product_name"
                        ,product_components.quantity                                AS "component_quantity"
                        ,(
                          SELECT products.um_id
                          FROM products
                          WHERE id = product_components.component_product_id
                          )                                                         AS "component_um_id"
                        ,(product_components.quantity / products.production_base) * porders_products.requested  AS "total"
                      FROM porders
                        ,porders_products
                        ,products
                        ,product_components
                      WHERE porders.id = porders_products.porder_id
                        AND porders_products.product_id = products.id
                        AND porders_products.product_id = product_components.product_id
                        AND porders.id IN (
                          SELECT rawm_audit_porders.porder_id
                          FROM rawm_audit_invtransferences
                            ,rawm_audits
                            ,rawm_audit_porders
                          WHERE rawm_audit_invtransferences.rawm_audits_id = rawm_audits.id
                            AND rawm_audit_porders.rawm_audits_id = rawm_audits.id
                            AND rawm_audit_invtransferences.invtransference_id IN
                            (
                              SELECT 
                                invtransferences.id
                              FROM invtransferences
                                ,invtransferences_products
                                ,products
                              WHERE invtransferences.id = invtransferences_products.invtransference_id
                                AND products.id = invtransferences_products.product_id
                                AND invtransferences.id IN (P_INVTRANSFERENCES_IDS)
                            )
                          )
                        AND product_components.component_product_id = p_product_id
                 ) LOOP
        IF(PORDER_PRODUCT.porder_id IS NOT NULL)THEN
            RETURN NEXT PORDER_PRODUCT;
        END IF;
    END LOOP;
    RETURN;         
END;
$body$
 VOLATILE
 COST 100
 ROWS 1000

我认为这里的错误是`invtransferences.id IN(P_INVTRANSFERENCES_IDS)

这是调用函数的选择:

SELECT  
      *
  FROM 
      get_invtransferences_porders_fporders(300001300 , array[300093753, 300094126, 300093349, 300093838, 300094128] )
  AS
  (
       "operation_type" varchar,
       "porder_id" numeric,
       "porder_user_id" numeric,
       "porder_user_name" varchar,
       "porder_delivery_datetime" date,
       "product_requested" numeric,
       "product_produced" numeric,
       "product_code" varchar,
       "product_name" varchar,
       "component_product_name" varchar,
       "component_quantity" numeric,
       "component_um_id" varchar,
       "total" numeric
   )
  ORDER BY
      "porder_id";

编辑:我删除了函数和调用函数的select中的VARIADIC单词

请你帮我。

1 个答案:

答案 0 :(得分:1)

您不需要将您的函数声明为VARIADIC以将数组传递给它。

试试这个

CREATE OR REPLACE FUNCTION xxx(
    p_product_id integer,
    P_INVTRANSFERENCES_IDS integer[])
  RETURNS SETOF record
  LANGUAGE sql
AS
$body$
    select p_product_id = ANY(P_INVTRANSFERENCES_IDS)
$body$;

请注意VARIADIC之前没有P_INVTRANSFERENCES_IDS

您还需要使用ANY代替IN来检查数组中的成员资格。

SqlFiddle