如何在oracle中查看过程的月份和年份

时间:2018-01-11 17:57:26

标签: oracle

所以我正在尝试在Oracle SQL开发人员中创建一个过程。该过程接收一年和一个月作为数字,然后我们从表中检查属性“日期”(其类型是DATE),如果月份和年份等于过程接收的那个,那么我们打印那张桌子。但是我遇到了很多错误,而且没有用。

create or replace procedure PR_SALE( month number, year number ) is
cursor c is
select v.OID_V, v.DATE, v.SHIPPING_COSTS
from ( SALES v inner join CLIENT c on ( v.OID_CL = c.OID_CL ) )
where MONTH(v.DATE) = month
and YEAR(v.DATE) = year
order by v.DATE;
fila c%rowtype;
begin
dbms_output.put_line('Sales list in  ' || month || ' from '|| year );
dbms_output.put_line(' ');
for fila in c loop
dbms_output.put_line(
rpad( fila.DATE, 20 ) ||
rpad( fila.SHIPPING COSTS, 20 ) ||
rpad( fila.OID_V, 20 )
);
end loop;
end ;

我收到以下错误。

Error(208,9): PLS-00341: declaration of cursor 'C' is incomplete or malformed
Error(209,2): PL/SQL: SQL Statement ignored
Error(214,7): PL/SQL: Item ignored
Error(219,2): PL/SQL: Statement ignored
Error(220,8): PLS-00364: loop index variable 'FILA' use is invalid

1 个答案:

答案 0 :(得分:0)

我就是这样做的:

create or replace procedure PR_SALE( p_month number, p_year number ) is
begin
    dbms_output.put_line('Sales list in  ' || p_month || ' from '|| p_year);
    dbms_output.put_line(' ');
    for fila in (select  v.OID_V, v.DATE, v.SHIPPING_COSTS
        from ( SALES v inner join CLIENT c on ( v.OID_CL = c.OID_CL ) )
        where to_number(to_char(v.DATE, 'MM')) = p_month 
        and to_number(to_char(v.DATE,'YYYY')) = p_year 
        order by v.DATE) 
    loop
        dbms_output.put_line(
        rpad( fila.DATE, 20 ) ||
        rpad( fila.SHIPPING COSTS, 20 ) ||
        rpad( fila.OID_V, 20 )
        );
    end loop;
end;
/

"一个月"和"年"是关键字,这就是为什么我用p_作为前缀。无论如何它可能会起作用,但使用关键字作为变量名称通常不是一种好习惯。