簡體   English   中英

將數組變量作為參數傳遞給另一個函數

[英]Pass array variable as parameter to another function

我在一個函數中初始化了兩個數組,一個數組用於數字(金額列表),另一個數組用於文本(發票列表)。 我想從這個函數中調用另一個函數,並將這兩個數組作為參數傳遞。
我可以這樣調用函數,一切正常:

select function_2('{110011-0115-7}'::text[], '{100}'::numeric[])

但是當我想發送一個保存文本數組的變量時,該數組似乎為空:

select function_2(ARRAY[invoice_list], ARRAY[amount_list]);

功能定義:

CREATE OR REPLACE FUNCTION function_1(OUT out_code1 integer
                                    , OUT out_message1 text)
  RETURNS RECORD AS
$BODY$
DECLARE
   counter      numeric;
   inv_cur      record;
   invoice_list text[];
   amount_list  numeric[]; 
BEGIN
   FOR inv_cur IN
      SELECT "ID", "AMOUNT"
      FROM "INVOICE"
      WHERE "ACCOUNT_ID" = in_account
   LOOP
       --Adding invoices to invoice array
        invoice_list[counter] := inv_cur."ID";

        --Adding amounts to amount array
        amount_list[counter] := inv_cur."AMOUNT";

        --Increasing counter for array indexes
        counter := counter + 1;
   END LOOP;

   --Caling other function
   SELECT * FROM function_2(ARRAY[invoice_list], ARRAY[amount_list]);
END
$BODY$
  LANGUAGE plpgsql VOLATILE

其他功能定義:

 CREATE OR REPLACE FUNCTION function_2(in_invoices text[]
                                     , in_amounts numeric[]) ...

您可以這樣修改功能:

CREATE OR REPLACE FUNCTION function_1(in_account int
                                , OUT out_code1 int
                                , OUT out_message1 text)
  RETURNS RECORD AS
$func$
DECLARE
   counter int := 1;  -- use int and initialize
   inv_cur      record;
   invoice_list text[];
   amount_list  numeric[]; 
BEGIN

   FOR inv_cur IN
      SELECT "ID", "AMOUNT"
      FROM   "INVOICE"
      WHERE  "ACCOUNT_ID" = in_account  -- !!!
      ORDER BY "ID" -- don't you care about sort order?
   LOOP
       --Adding invoices to invoice array
        invoice_list[counter] := inv_cur."ID";

        --Adding amounts to amount array
        amount_list[counter] := inv_cur."AMOUNT";

        --Increasing counter for array indexes
        counter := counter + 1;
   END LOOP;

   --  Calling other function
   SELECT f.outfield_1, f.outfield_2  -- replace with actual names!
   INTO out_code1, out_message1 FROM function_2(invoice_list, amount_list) f;

END
$func$  LANGUAGE plpgsql VOLATILE

筆記

  • 必須將函數變量計數器初始化或將其設置為NULL。

  • in_account解釋為表"INVOICE"列名(可能不是)。 似乎缺少功能參數。

  • 用您想要的實際排序順序替換我添加的ORDER BY "ID"

  • 需要分配最終SELECT的結果。

  • invoice_listamount_list已經是數組。 如果要添加另一個數組維,則僅將它們包裝到另一個ARRAY層中(我對此表示懷疑。)

現在該功能應該起作用了 仍然是昂貴的廢話 ...

數組處理方式非常昂貴。 循環也很昂貴。 用以下查詢替換function_1()

SELECT f.*
FROM  (
   SELECT function_2(array_agg("ID"), array_agg("AMOUNT")) AS f
   FROM  (
      SELECT "ID", "AMOUNT"
      FROM   "INVOICE"
      WHERE  "ACCOUNT_ID" = in_account  -- your input here!
      ORDER  BY "ID"
      ) t1
   ) t2;

您可以使用單個查詢級別:

SELECT (function_2(array_agg("ID" ORDER BY "ID")
                 , array_agg("AMOUNT" ORDER BY "ID"))).*
FROM   "INVOICE"
WHERE  "ACCOUNT_ID" = in_account;

但是性能會差很多。 帶有子查詢的版本只能排序一次,並且也只能調用一次函數:

如果需要,可以將其包裝到SQL函數中。

暫無
暫無

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

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