如何在oracle中创建和执行只有月份和年份的过程?

时间:2016-01-25 16:31:13

标签: oracle

我必须创建一个月和一年的程序,并显示在指定月份和年份购买的所有客户。程序成功完成,但是当我执行它时,oracle显示错误:01843。00000 - "不是有效的月份"。我来自巴西,所以我的变量是葡萄牙语:

/* I am brazilian, sorry my mistakes in english */

create or replace procedure P_data (p_data VENDA3.dt_venda%type)

IS

 data_venda VENDA3.dt_venda%type;
   nome CLIENTE3.nome_cliente%type;
  cursor cur_vendas3 is select V.dt_venda, C.nome_cliente from CLIENTE3 C, VENDA3 V where
 V.cliente_cd_cliente = C.cd_cliente and 
V.dt_venda = to_date(p_data,'MM/YYYY');

begin

 if cur_vendas3%isopen then
  close cur_vendas3;
   end if;
open cur_vendas3;

loop

   fetch cur_vendas3 into data_venda, nome;
  exit when cur_vendas3%notfound;
   dbms_output.put_line(data_venda ||'-'|| nome);

end loop;
close cur_vendas3; 

end;

exec P_dataCompra('19/01/2016'); -- This is the format year in Brazil

1 个答案:

答案 0 :(得分:0)

尝试以下方法:

CREATE OR REPLACE PROCEDURE P_data(p_data IN VARCHAR2) IS
data_venda    VENDA3.dt_venda%TYPE;
nome          CLIENTE3.nome_cliente%TYPE;

CURSOR cur_vendas3  IS
    SELECT V.dt_venda, C.nome_cliente
      FROM CLIENTE3 C, VENDA3 V
     WHERE     V.cliente_cd_cliente = C.cd_cliente
           AND TO_CHAR(V.dt_venda, 'MM/YYYY') = p_data;
BEGIN
IF cur_vendas3%ISOPEN THEN
    CLOSE cur_vendas3;
END IF;

OPEN cur_vendas3;

LOOP
    FETCH cur_vendas3 INTO data_venda, nome;

    EXIT WHEN cur_vendas3%NOTFOUND;
    DBMS_OUTPUT.put_line(TO_CHAR(data_venda, 'DD/MM/YYYY') || '-' || nome);
END LOOP;

CLOSE cur_vendas3;
END;

exec P_dataCompra('01/2016');