簡體   English   中英

PostgreSQL:如何傳遞和數組給函數,以及如何在查詢中使用IN運算符

[英]PostgreSQL: How to pass and array to a function and using it in a query with the IN operator

我有一個問題,我想將數組傳遞給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";

編輯 :我刪除了函數中和調用該函數的選擇中的VARIADIC單詞

你能幫我嗎

您無需將函數聲明為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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM