简体   繁体   中英

Why does this function always return 0

I don't know why this function always returns 0

CREATE OR REPLACE FUNCTION QTYDEPOT(
    p_org_id     IN NUMBER,
    p_product_id IN NUMBER,
    p_datefrom   IN DATE,
    p_dateto     IN DATE)
  RETURN NUMBER
AS
  qty NUMBER;
BEGIN
  SELECT COALESCE(SUM(C_InvoiceLine.qtyinvoiced), 0)
  INTO qty
  FROM C_InvoiceLine
  INNER JOIN C_invoice
  ON (c_invoiceline.C_INVOICE_ID = c_invoice.C_INVOICE_ID)
  INNER JOIN C_BPartner
  ON (c_invoice.C_BPARTNER_ID   = c_bpartner.C_BPARTNER_ID)
  WHERE C_BPartner.ISSALESREP   = 'N'
  AND C_BPartner.ISEMPLOYEE     = 'N'
  AND c_bpartner.ISCUSTOMER     = 'Y'
  AND c_invoiceline.AD_org_id   = p_org_id
  AND c_invoiceline.m_product_id= p_product_id
  AND c_invoice.DateInvoiced BETWEEN p_datefrom AND p_dateto;
  RETURN qty ;
END;

PS : if I remove the date part of the close where

c_invoice.DateInvoiced BETWEEN p_datefrom AND p_dateto;

The function returns the real values.

I call it like this

SELECT  
   ..
   QTYDEPOT( 1000000, p.m_product_id,'7/7/2014','24/7/2014') as qtyDepot

try this:

SELECT  
   ..
   QTYDEPOT( 1000000, p.m_product_id,to_date('7/7/2014','dd/mm/yyyy'),to_date('24/7/2014','dd/mm/yyyy')) as qtyDepot

you have to specify the date format you are passing to the function,

hope this helps!

You have problems with DATE manipulation. I would suggest you to try calling your function like this :

SELECT  
   ..
   QTYDEPOT( 1000000, p.m_product_id,DATE('2014-07-07'),DATE('2014-07-24')) as qtyDepot

AS you can see, Oracle standard format is 'yyyy-mm-dd' I don't know if DATE(...) is needed, but I use to manipulate DATE like this: code is clearer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM