PL/SQL 过程异常编译错误

PL/SQL procedure exception compile error

我想编写在 temp_line 中临时 table 中创建并在 temp_line 中循环检查并获取异常的程序。

create or replace package body cux_bpm_hr_030_040_2 is 
FUNCTION save_hr_030_040(p_event_record_id       number,
                       p_share_log_id          number,
                       p_oa_flow_doc_header_id number,
                       p_created_by            number) return number is
v_data_line_id        number;
v_total_amount        number;
v_annual_vct_hour     number;
v_ot_vct_hour         number;
v_annual_vct_req_hour number;
v_ot_vct_req_hour     number;
v_string_date         varchar(100);
e_req_hour_error exception;
e_req_hour_error1 exception;
v_num                 number;

begin
select count(*) 
into v_num 
from user_tables 
where table_name = 'temp_line';

if v_num < 1 then   execute immediate ('CREATE GLOBAL TEMPORARY TABLE temp_line (   holiday_id VARCHAR2(6), holiday_begin_date DATE, holiday_begin_hour (6), holiday_begin_min (6), holiday_end_date DATE, holiday_end_hour (6), holiday_end_min (6)   ) ON COMMIT PRESERVE ROWS');   end if;

execute immediate ('FOR recitem IN(select * from oa_tplt019_line_01_v l where l.oa_flow_doc_header_id = p_oa_flow_doc_header_id)


LOOP 
insert into temp_line(holiday_id,holiday_begin_date,holiday_begin_hour,holiday_begin_min,holiday_end_date, holiday_end_hour,holiday_end_min)
values
(recitem.C_025,recitem.C_027,recitem.C_028,recitem.C_029,recitem.C_030,recitem.D_001,reitem.D_002)  ENDLOOP;');`

LOOP
Declare
        begin_diff varchar2(8);
        diff       varchar2(8);
        holiday_begin_date   number;
        holiday_end_date     number;
select nvl(l.holiday_begin_date, 0),nvl(l.holiday_end_date, 0)
into holiday_begin_date,holiday_end_date
from temp_line l

begin_diff = holiday_begin_date - sys_date
diff =  holiday_end_date - holiday_begin_date
IF begin_diff > 3 then
  raise e_req_hour_error;
  ENDIF;
IF diff < 0 then
  raise e_req_hour_error1;
  ENDIF; ENDLOOP;

我遇到编译错误:

Compilation errors for PACKAGE BODY KL_BPMDEV.CUX_BPM_HR_030_040_2

Error: PLS-00103:  Encountered the symbol ""when expecting one of the following:
        ( begin case declare end
          exception exit for goto if loop mod null pragma raise return
          select update while with <an identifier>
          <a double-quoted delimited-identifier> <a bind variable> <<
          continue close current delete fetch lock insert open rollback
          savepoint set sql execute commit forall merge pipe purge
Line: 25
Text: ﹛ --create temporary table

代码中的主要错误是您在 BEGIN ... END 中声明了一些其他变量。您只能在函数开始之前声明。你需要学习写作的基础functions/procedures。参见 here

除此之外,您还有几个调试错误需要修复。下面的函数已经编译成功。请看我更正的评论

create or replace FUNCTION save_hr_030_040(p_event_record_id       number,
                       p_share_log_id          number,
                       p_oa_flow_doc_header_id number,
                       p_created_by            number) return number is
v_data_line_id        number;
v_total_amount        number;
v_annual_vct_hour     number;
v_ot_vct_hour         number;
v_annual_vct_req_hour number;
v_ot_vct_req_hour     number;
v_string_date         varchar(100);
e_req_hour_error exception;
e_req_hour_error1 exception;
v_num                 number;
    begin_diff varchar2(8);     --You cannot declare inside BEGIN/END, you can only declare in the declaration section
    diff       varchar2(8);
    holiday_begin_date   number;
    holiday_end_date     number;

begin
select count(*) 
into v_num 
from user_tables 
where table_name = 'temp_line';
if v_num < 1 then 
if v_num < 1 then 
  execute immediate 'CREATE GLOBAL TEMPORARY TABLE temp_line ( 
   holiday_id VARCHAR2(6),
   holiday_begin_date DATE,
   holiday_begin_hour VARCHAR2(6),
   holiday_begin_min VARCHAR2(6),
   holiday_end_date DATE,
   holiday_end_hour VARCHAR2(6),
   holiday_end_min VARCHAR2(6)
  ) ON COMMIT PRESERVE ROWS'; 
  end if;
execute immediate ('FOR recitem IN(select * from oa_tplt019_line_01_v l where l.oa_flow_doc_header_id = p_oa_flow_doc_header_id); 


LOOP 
insert into temp_line(holiday_id,holiday_begin_date,holiday_begin_hour,holiday_begin_min,holiday_end_date, holiday_end_hour,holiday_end_min)
values
(recitem.C_025,recitem.C_027,recitem.C_028,recitem.C_029,recitem.C_030,recitem.D_001,reitem.D_002)  ENDLOOP;');   --remove extra '`' 

LOOP
select nvl(l.holiday_begin_date, 0),nvl(l.holiday_end_date, 0)
into holiday_begin_date,holiday_end_date
from temp_line l;

begin_diff := holiday_begin_date - sys_date;   --missing semi colon
diff :=  holiday_end_date - holiday_begin_date; --missing semi colon
IF begin_diff > 3 then
  raise e_req_hour_error;
  END IF;
IF diff < 0 then
  raise e_req_hour_error1;
  END IF; END LOOP;   --These are not ENDIF and ENDLOOP, you need to add space betwween them

      end if;
      end;

无论您是复制部分代码,还是完全由您自己编写,都需要使用您的编辑器来修复这些类型的错误。我看到你标记了 plsqldeveloper。该工具会准确告诉您存在哪些错误,您可以开始一项一项地修复它们,直到编译完成。