如何在 Postgres 中执行函数

How to execute functions in Postgres

如何在 Postgres 中执行函数?

我尝试了以下方法:

select pricelimit();

但它给我的错误如下:

No function matches the given name and argument types. You might need to add explicit type casts.

这是函数:

CREATE OR REPLACE FUNCTION pricelimit(
    p_product_id numeric,
    p_pricelist_version_id numeric)
  RETURNS numeric AS
$BODY$
DECLARE
    v_Price     numeric;
    v_ProductPrice  numeric;
    bom     record;
BEGIN
    --  Try to get price from PriceList directly
    SELECT  COALESCE (SUM(PriceLimit), 0)
        INTO    v_Price
    FROM    M_PRODUCTPRICE
    WHERE M_PriceList_Version_ID=p_PriceList_Version_ID AND M_Product_ID=p_Product_ID;
    IF (v_Price = 0) THEN
        FOR bom in SELECT bl.M_Product_ID AS M_ProductBOM_ID, 
            CASE WHEN bl.IsQtyPercentage = 'N' THEN bl.QtyBOM ELSE bl.QtyBatch / 100 END AS BomQty , p.IsBOM 
        FROM PP_PRODUCT_BOM b
        INNER JOIN M_PRODUCT p ON (p.M_Product_ID=b.M_Product_ID)
        INNER JOIN PP_PRODUCT_BOMLINE bl ON (bl.PP_Product_BOM_ID=b.PP_Product_BOM_ID)
        WHERE b.M_Product_ID = p_Product_ID
        LOOP
            v_ProductPrice := Bompricelimit (bom.M_ProductBOM_ID, p_PriceList_Version_ID);
            v_Price := v_Price + (bom.BOMQty * v_ProductPrice);
        END LOOP;
    END IF;
    --
    RETURN v_Price;
END;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;

函数存在于数据库中 我该如何解决这个问题请帮助我

你的函数需要两个参数,但你调用它时没有任何参数。

你需要这样称呼它:

select pricelimit(4, 2);

其中 4 是参数 p_product_id 的值,2 是参数 p_pricelist_version_id

的值

有关更多示例和详细信息,请参阅手册:
https://www.postgresql.org/docs/current/static/sql-createfunction.html#SQL-CREATEFUNCTION-EXAMPLES

根据您的函数定义

CREATE OR REPLACE FUNCTION pricelimit(
    p_product_id numeric,
    p_pricelist_version_id numeric);

函数调用就是这样

select pricelimit(10, 4);

您必须传递参数 p_product_id 和 p_pricelist_version_id。

如果你想从你的参数传递默认值,我们必须改变函数定义如下:

CREATE OR REPLACE FUNCTION pricelimit(
    p_product_id numeric default 0::numeric,
    p_pricelist_version_id numeric default 0::numeric)
  RETURNS numeric AS
$BODY$
DECLARE
    v_Price     numeric;
    v_ProductPrice  numeric;
    bom     record;
BEGIN

    --function Defination
END;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;

然后你可以这样调用函数

select pricelimit();

希望你的疑惑得到解决....