Search code examples
postgresqlplpgsqlstored-functions

How to execute functions in Postgres


How can I execute a function in Postgres?

I tried the following:

select pricelimit();

But it is giving me error which is as follow:

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

This is the function:

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;

and function is present in the database How can I solve this problem please help me


Solution

  • Your function requires two parameters but you call it without any.

    you need to call it like this:

    select pricelimit(4, 2);
    

    Where 4 is the value for the parameter p_product_id and 2 is the value for the parameter p_pricelist_version_id

    See the manual for more examples and details:
    https://www.postgresql.org/docs/current/static/sql-createfunction.html#SQL-CREATEFUNCTION-EXAMPLES