PostgreSQL中的汇编功能问题

时间:2019-01-04 18:19:41

标签: postgresql plpgsql

    -- Function: adempiere.qtyondateorg2(integer, timestamp with time zone, character varying)

-- DROP FUNCTION adempiere.qtyondateorg2(integer, timestamp with time zone, character varying);

CREATE OR REPLACE FUNCTION adempiere.qtyondateorg2(
    product_id integer,
    indate timestamp with time zone,
    org_id character varying)
  RETURNS numeric AS
$BODY$
DECLARE
    Quantity        NUMERIC := 0;
    vP              NUMERIC := 0;
BEGIN

  SELECT Aux.M_Product_ID, SUM(Aux.QtyOnHand) AS QtyOnHand 
    INTO vP, Quantity FROM

  (SELECT DISTINCT    '1', s.M_Product_ID, SUM(s.QtyOnHand) AS QtyOnHand, l.M_Warehouse_ID, l.M_Locator_ID
          FROM    M_Storage s
    INNER JOIN    M_Locator l ON s.M_Locator_ID = l.M_Locator_ID
    INNER JOIN    M_Warehouse w ON l.M_Warehouse_ID=w.M_Warehouse_ID
         WHERE    s.M_Product_ID = Product_ID
         AND      w.lbr_WarehouseType NOT LIKE '3RD'
        AND l.AD_Org_ID IN (org_id)
      GROUP BY    s.M_Product_ID, l.M_Warehouse_ID, l.M_Locator_ID, l.AD_Org_ID
UNION
        SELECT    '2', t.M_Product_ID, SUM(t.MovementQty) * -1 AS QtyOnHand, l.M_Warehouse_ID, t.M_Locator_ID
          FROM    M_Transaction t
    INNER JOIN    M_Locator l ON t.M_Locator_ID = l.M_Locator_ID
    INNER JOIN    M_Warehouse w ON l.M_Warehouse_ID=w.M_Warehouse_ID
         WHERE    TRUNC(t.MovementDate) >= TRUNC(InDate)
           AND    w.lbr_WarehouseType NOT LIKE '3RD'
           AND    t.M_Product_ID = Product_ID
           AND l.AD_Org_ID IN ( org_id )
      GROUP BY    t.M_Product_ID, l.M_Warehouse_ID, t.M_Locator_ID, l.AD_Org_ID) Aux

      GROUP BY M_Product_ID;

      IF (Quantity IS NULL) THEN
    Quantity := 0;
      END IF;

      RETURN Quantity;
END
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;
ALTER FUNCTION adempiere.qtyondateorg2(integer, timestamp with time zone, character varying)
  OWNER TO adempiere; 

我在以下函数中遇到问题,在参数AD_Org_ID中,我想按如下所示作为字符串传递:

  select adempiere.qtyondateorg2 (2045480, '31/12/2018',' 2000002, 2000003');

但是我遇到以下错误:

错误:运算符不存在:数值=字符变化 第10行:AND l.AD_Org_ID IN(org_id)

提示:没有运算符匹配给定的名称和参数类型。您可能需要添加显式类型转换。

对以正确方式形成功能的任何帮助吗?谢谢

1 个答案:

答案 0 :(得分:0)

您不能以这种方式使用IN运算符:

create or replace function wrong_func(org_id varchar)
returns boolean language plpgsql as $$
declare
    ad_org_id numeric = 1;
begin
    -- this raises an error as ad_org_id is numeric and org_id is varchar
    return ad_org_id in (org_id);
end $$;

select wrong_func('1,2,3');

ERROR:  operator does not exist: numeric = character varying
LINE 1: SELECT ad_org_id in (org_id)
                         ^
HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.  

使用数字数组作为参数,如下所示:

create or replace function correct_func(org_id numeric[])
returns boolean language plpgsql as $$
declare
    ad_org_id numeric = 1;
begin
    return ad_org_id = any(org_id);
end $$;

select correct_func('{1,2,3}')

 correct_func 
--------------
 t
(1 row)