Oracle PL/SQL 错误 - 存储过程 - ORA-00979 和 ORA-06512

Oracle PL/SQL error - store procedure - ORA-00979 and ORA-06512

我正在使用以下存储过程将数据插入 table。

create or replace PROCEDURE PM
(
  date1 in varchar2
 ,date2 in varchar2
 ,date3 in varchar2
) AS

cursor cur_cd is
(
  select to_date(date1,'DD-MON-YY') as date1
          ,trim(t.DEPT_CODE) DEPT_CODE
          ,count(t.DEPT_CODE) count_dept
          ,sum(t.amount) amount
  from department t
  where t.date >= to_date(date2,'DD-MON-YY') 
  and t.date <= to_date(date2,'DD-MON-YY')
  and t.dept_name like 'finance%' 
  and (trim(t.DT_code)='TR_01' or t.DT_file like 'DTF_20%')
  and t.DEPT_CODE not in ('HR','ADMIN','ACADEMIC')
  group by t.DEPT_CODE 
);

Type rec_set is table of dept_file%rowtype;
    v_rec_set record_set;
    begin
        open cur_cd;
        loop
        fetch cur_cd
           bulk collect into v_rec_set limit 100;
           exit when v_rec_set.count()=0;
     
        begin
          forall i in v_rec_set.first..v_rec_set.last
          insert into dept_file
          values v_rec_set(i);
        end;
      end loop;
      close cur_cd;

    exception when others then raise;
end PM;

执行程序时出现运行时错误。但是手动执行查询没有错误。

ORA-000979 : not a GROUP BY expression
ORA-006512 : at "ABS.PM", line 9

此外,当对参数(date1、date2 和 date3)进行硬编码时,程序可以正常运行。

你能帮我解决这个错误吗?

所有 non-aggregated 列必须在 GROUP BY 子句中指定。因此:

GROUP BY TO_DATE (date1, 'DD-MON-YY'), TRIM (t.dept_code)

顺便问一下,您真的将日期值存储为字符串吗?我为什么要问?因为您在整个代码中都使用了 TO_DATE 函数。如果你是,那么以后尽量不要这样做。 Oracle 提供 DATE 数据类型,您应该使用它。