为什么这个函数总是返回0

时间:2014-08-05 07:57:55

标签: sql oracle select plsql stored-functions

我不知道为什么这个函数总是返回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;

P.S:如果我删除了关闭where

的日期部分
c_invoice.DateInvoiced BETWEEN p_datefrom AND p_dateto;

该函数返回实际值。

我称之为

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

2 个答案:

答案 0 :(得分:0)

试试这个:

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

你必须指定传递给函数的日期格式,

希望这有帮助!

答案 1 :(得分:-1)

DATE操作存在问题。我建议你尝试这样调用你的函数:

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

如您所见,Oracle标准格式为'yyyy-mm-dd'我不知道是否需要DATE(...),但我会像这样操纵DATE:代码更清晰。