使用PL / SQL的日期和条件

时间:2015-12-23 20:24:51

标签: stored-procedures plsql cursor

我遇到下面的过程有问题,因为每当我传递月份和年份的日期时,IF条件会循环显示在我的表中的记录数 - 只存在两行 - 并且它还显示了两种可能性。我还把结果包括在内以表明我的意思!

 PRODUCT_NUM   ORDER_NUM    QUANTITY        PRICE     MONTHLY_DATE
-------------- ----------- ------------- ------------ -------------
   12345         106           3             19.99     21-DEC-15   
   67894         107           1             19.99     21-DEC-15


 CREATE OR REPLACE PROCEDURE Proce_Name (Pram_Date DATE ) 
   AS
 BGIN
  FOR Y IN 
    (SELECT SUM(PRICE) AS TOTAL_AMOUNT, SUM(NVL(QUANTITY,0) * NVL (   
    (PRICE,0)) AS TOTAL, MONTHLY_DATE   
    FROM PRODUCTS
    GROUP BY MONTHLY_DATE 
    ORDER BY MONTHLY_DATE DESC) LOOP
    IF  TO_CHAR( Y.MONTHLY_DATE, 'mm-yyyy')= TO_CHAR(Pram_Date,'mm-yyyy') THEN
     DBMS_OUTPUT.PUT_LINE('TOTAL AMOUNT: '|| '  '|| Y.TOTAL_AMOUNT);
     DBMS_OUTPUT.PUT_LINE('');
     DBMS_OUTPUT.PUT_LINE('TOTAL: '|| '   '||  Y.TOTAL);
   ELSE
     DBMS_OUTPUT.PUT_LINE('No Data for this month');
   END IF;
  END LOOP;

END Proce_name;

调用块:

DECLARE
   MONTHLY_D DATE := TO_DATE('01-2012','mm-yyyy');
BEGIN
 Proce_Name(MONTHLY_D);
END;

/

这是我传递的日期与表格中的记录匹配的时间:

     No Data for this month

     TOTAL AMOUNT:   4

     TOTAL:      99.96

当我传递表格中不存在的日期时的结果:

     No Data for this month
     No Data for this month

如何更改我的程序以准确检查上述日期格式?

enter image description here

3 个答案:

答案 0 :(得分:0)

我有以下更新程序

CREATE OR REPLACE PROCEDURE get_sales_report(c_date date) AS
  l_amount number(18, 2);
  l_total  number(18, 2);
BEGIN
  begin
    SELECT SUM(PRICE), SUM(NVL(QUANTITY, 0) * NVL(PRICE, 0))
      into l_amount, l_total
      FROM product
     where TO_CHAR(MONTHLY_DATE, 'mm-yyyy') = to_char(c_date, 'mm-yyyy')
     GROUP BY TO_CHAR(MONTHLY_DATE, 'mm-yyyy')
     ORDER BY TO_CHAR(MONTHLY_DATE, 'mm-yyyy') DESC;

    DBMS_OUTPUT.PUT_LINE('TOTAL AMOUNT: ' || '  ' || l_amount);
    DBMS_OUTPUT.PUT_LINE('');
    DBMS_OUTPUT.PUT_LINE('TOTAL: ' || '   ' || l_total);
  exception
    when no_data_found then
      DBMS_OUTPUT.PUT_LINE('No Data for this month');
  END; -- end of exception
  -- add your code here
  -- add your code here
  -- add your code here
end;

还为dbms_output回显文本调用以下命令。

set serveroutput on size 30000;

将您的程序称为

declare
  c_date2 DATE := TO_DATE('12-2015','mm-yyyy');
begin
  get_sales_report(c_date2);
end;

答案 1 :(得分:0)

以下是代码,您也可以考虑异常处理: 很少有观察,你认为prem_date是自己的char而不是传递日期。此外,如果数据集很大并且monthly_date已编制索引,则应避免在monthly_date上应用to_char条件。

CREATE OR REPLACE PROCEDURE Proce_Name (Pram_Date DATE )
   AS
  l_total_amount float;
  l_total float;
 BEGIN
    SELECT SUM(PRICE) AS TOTAL_AMOUNT,
     SUM(NVL(QUANTITY,0) * NVL(PRICE,0)) AS TOTAL
    INTO l_total_amount, l_total
    FROM PRODUCTS
    WHERE to_char(monthly_date, 'mm-yyyy') = to_char(pram_date, 'mm-yyyy');
    if(l_total_amount is not null) then
      DBMS_OUTPUT.PUT_LINE('TOTAL AMOUNT: '|| '  '|| L_TOTAL_AMOUNT);
      DBMS_OUTPUT.PUT_LINE('');
      DBMS_OUTPUT.PUT_LINE('TOTAL: '|| '   '||  L_TOTAL);
    else
      DBMS_OUTPUT.PUT_LINE('No Data for this month');
    end if;
END Proce_name;
/

答案 2 :(得分:0)

日期是固定大小的类型。因此,如果要按整月分组,则必须将日期列查询为char。这意味着,您应该使用TO_CHAR而不是TO_DATE。

这是(更简单)的方法(我的更改是小写的) -

CREATE OR REPLACE PROCEDURE Proce_Name (Pram_Date date) AS   
  l_month        char(7);
  l_total_amount number;
  l_total        number;
BEGIN
  SELECT to_char(monthly_date,'mm-yyyy') as month, 
          SUM(PRICE) AS TOTAL_AMOUNT, 
          SUM(NVL(QUANTITY,0)*NVL(PRICE,0)) AS TOTAL   
   INTO l_month, l_total_amount, l_total
   FROM PRODUCTS
   where to_char(monthly_date,'mm-yyyy')=to_char(Pram_Date, 'mm-yyyy')
   GROUP BY to_char(monthly_date,'mm-yyyy');

  if l_month is not null then
    DBMS_OUTPUT.PUT_LINE('TOTAL AMOUNT: '|| '  '|| Y.TOTAL_AMOUNT);
    DBMS_OUTPUT.PUT_LINE('');
    DBMS_OUTPUT.PUT_LINE('TOTAL: '|| '   '||  Y.TOTAL);  
  else
    DBMS_OUTPUT.PUT_LINE('No Data for this month');
  end if;   

END Proce_name;
/

以下是一些测试代码 - http://sqlfiddle.com/#!4/b1068/2