prc_file_read_pr, prc_file_write_pr - bami74/me GitHub Wiki

create or replace procedure prc_file_read_pr(p_app_code out number,
                                             p_app_msg  out varchar2) as
  file_handle sys.utl_file.file_type; -- file handle of OS flat
  v_sql_msg   varchar2(200);
  e_not_completed exception;
  v_filename       varchar2(100);
  v_cnt            number := 0; -- Line Count
  v_line           varchar2(5000); -- Line Text
  v_complete_count number := 0; -- Insert Row Count
  v_error_count    number := 0; -- Insert Row Count
  v_msg            varchar2(100);
  v_err_col        number;

  v_erp_id     varchar2(50);
  v_mfc        varchar2(50);
  v_equip_name varchar2(50);
  v_cfg_type   varchar2(50);
  v_fa         varchar2(50);
  v_eng_type   varchar2(50);
  v_project_id varchar2(50);
  v_open_date  varchar2(50);
  v_stdate     date;
  v_allstring  varchar2(200);
begin

  -- ์ •์ƒ์ ์ธ ๋ฐ์ดํ„ฐ๋Š” prc_list_info_tb ์— ๋“ค์–ด๊ฐ€๊ฒŒ ๋˜๊ณ 
  -- ๋น„์ •์ƒ์ ์ธ ๋ฐ์ดํ„ฐ๋Š” ???_tb ์— ๋“ค์–ด๊ฐ€๊ฒŒ ๋œ๋‹ค.

  v_stdate := sysdate;

  delete from prc_list_info_tb;

  v_stdate := sysdate;

  commit;
  ----------------------------------------------------------------------------------------------
  v_filename := 'if_list1.txt';
  --============================================================================================
  -- PCode File ์„ ์ฝ์–ด์„œ Table์— Insert
  --============================================================================================
  -- File Open ... Read
  file_handle      := sys.utl_file.fopen('DIR1', v_filename, 'r');
  v_error_count    := 0;
  v_complete_count := 0;

  loop
  
    -- Line Text Read
    sys.utl_file.get_line(file_handle, v_line);
  
    v_msg := 'Error  !!';
  
    v_allstring := v_line;
    if instr(v_line, '|') > 0 then
      -- ์ฒซ๋ฒˆ์งธ Line์€ ์ œ๋ชฉ ์ด๋ฏ€๋กœ Skip ํ•œ๋‹ค.
      -- ๋ฌธ์ž์—ด์ค‘์—  ์„ธ๋ถ€์ฃผ์†Œ์— ?๊ฐ€ ๋“ค์–ด์˜ค๋Š” ๋ถ€๋ถ„์—์„œ ๊ตฌ๋ถ„์ž์ธ | ๋ฅผ ์žƒ์–ด๋ฒ„๋ ค, ์ปค๋Ÿผ์ด ํ•˜๋‚˜์”ฉ ๋‹น๊ฒจ์ง€๋Š”๋ฐ,
      -- ์›๋ž˜๋Š” ERP ์—์„œ ์ •์ƒ์ ์œผ๋กœ ๋“ค์˜ค์™€์•ผ ํ•˜์ง€๋งŒ, ๋ณด์ •์ฐจ์›์—์„œ ์ด๋ฅผ ์ˆ˜์ •ํ•˜๋„๋ก ํ•จ.
      select length(v_line) - length(replace(v_line, '|', ''))
        into v_cnt
        from dual;
    
      if v_cnt <> 48 then
        v_cnt  := v_cnt;
        v_line := replace(v_line, '?', '|');
      end if;
    
      v_err_col := 1;
      v_erp_id  := substr(v_line, 1, instr(v_line, '|') - 1);
    
      v_err_col := v_err_col + 1;
      v_line    := substr(v_line, instr(v_line, '|') + 1);
      v_mfc     := substr(v_line, 1, instr(v_line, '|') - 1);
    
      v_err_col    := v_err_col + 1;
      v_line       := substr(v_line, instr(v_line, '|') + 1);
      v_equip_name := substr(v_line, 1, instr(v_line, '|') - 1);
    
      v_err_col  := v_err_col + 1;
      v_line     := substr(v_line, instr(v_line, '|') + 1);
      v_cfg_type := substr(v_line, 1, instr(v_line, '|') - 1);
    
      v_err_col := v_err_col + 1;
      v_line    := substr(v_line, instr(v_line, '|') + 1);
      v_fa      := substr(v_line, 1, instr(v_line, '|') - 1);
    
      v_err_col  := v_err_col + 1;
      v_line     := substr(v_line, instr(v_line, '|') + 1);
      v_eng_type := substr(v_line, 1, instr(v_line, '|') - 1);
    
      v_err_col    := v_err_col + 1;
      v_line       := substr(v_line, instr(v_line, '|') + 1);
      v_project_id := substr(v_line, 1, instr(v_line, '|') - 1);
    
      v_err_col   := v_err_col + 1;
      v_line      := substr(v_line, instr(v_line, '|') + 1);
      v_open_date := substr(v_line, 1, instr(v_line, '|') - 1);
    
      /*      v_err_col := v_err_col + 1;
      v_line    := substr(v_line, instr(v_line, '|') + 1);
      v_empty   := v_line;*/
    
      -- Insert of Line Text
    
      begin
        insert into prc_list_info_tb
          (erp_id,
           mfc,
           equip_name,
           cfg_type,
           fa,
           eng_type,
           project_id,
           open_date)
          select v_erp_id,
                 v_mfc,
                 v_equip_name,
                 v_cfg_type,
                 v_fa,
                 v_eng_type,
                 v_project_id,
                 v_open_date
          
            from dual;
      
        v_complete_count := v_complete_count + 1;
      exception
        when others then
        
          v_sql_msg := substr(sqlerrm, 1, 200);
        
      end;
    
    elsif instr(v_line, '|') = 0 then
      -- ๋งˆ์ง€๋ง‰ Line์€ ์ด ๋ผ์ธ์ˆ˜์ด๋‹ค. ํ•ด๋‹นํŒŒ์ผ์˜ ๋์ด๊ธฐ๋„ ํ•˜๋‹ค
      exit;
    end if;
  
    -- Line Count
    v_cnt := v_cnt + 1;
    -- Line ๋ณ€์ˆ˜ Clear
    v_line      := '';
    v_allstring := v_line;
    v_msg       := 'Complete Process !!';
  end loop;

  -- File CLose
  sys.utl_file.fclose(file_handle);

  v_stdate := sysdate;

  ----------------------------------------------------------------------------------------------

  p_app_code := 0;
  p_app_msg  := 'File Line = ' || to_char(v_cnt) || ', Complete Line = ' ||
                to_char(v_complete_count) || ' Error Line = ' ||
                to_char(v_error_count);

  commit;

exception
  when sys.utl_file.invalid_filehandle then
    rollback;
    if sys.utl_file.is_open(file_handle) then
      sys.utl_file.fclose(file_handle);
    end if;
    p_app_code := sys.utl_file.invalid_filehandle_errcode;
    p_app_msg  := 'invalid_filehandle';
  when sys.utl_file.invalid_maxlinesize then
    rollback;
    if sys.utl_file.is_open(file_handle) then
      sys.utl_file.fclose(file_handle);
    end if;
    p_app_code := sys.utl_file.invalid_maxlinesize_errcode;
    p_app_msg  := 'invalid_maxlinesize';
  when sys.utl_file.invalid_path then
    rollback;
    if sys.utl_file.is_open(file_handle) then
      sys.utl_file.fclose(file_handle);
    end if;
    p_app_code := sys.utl_file.invalid_path_errcode;
    p_app_msg  := 'invalid_path';
  when sys.utl_file.invalid_mode then
    rollback;
    if sys.utl_file.is_open(file_handle) then
      sys.utl_file.fclose(file_handle);
    end if;
    p_app_code := sys.utl_file.invalid_mode_errcode;
    p_app_msg  := 'invalid_mode';
  when sys.utl_file.read_error then
    rollback;
    if sys.utl_file.is_open(file_handle) then
      sys.utl_file.fclose(file_handle);
    end if;
    p_app_code := sys.utl_file.read_error_errcode;
    p_app_msg  := 'read_error';
  when others then
  
    if sys.utl_file.is_open(file_handle) then
      sys.utl_file.fclose(file_handle);
    
    end if;
  
    commit;
    p_app_code := 0;
    p_app_code := 0;
    p_app_msg  := v_msg || ' File Line = ' || to_char(v_cnt) ||
                  ', Complete Line = ' || to_char(v_complete_count) ||
                  ' Error Line = ' || to_char(v_error_count - 1);
    -- ์—๋Ÿฌ๋ผ์ธ์ˆ˜๋ฅผ ํ•˜๋‚˜ ๋บ€๊ฒƒ์€ ์ฒซ ๋ผ์ธ์€ ํ—ค๋”์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

end prc_file_read_pr;
create or replace procedure prc_file_write_pr(p_app_code out number,
                                                      p_app_msg  out varchar2) as
  file_handle sys.utl_file.file_type; -- file handle of OS flat
  v_filename  varchar2(100);
  v_cnt       number := 0; -- Line Count
  v_line      varchar2(5000); -- Line Text
  v_row_count number := 0; -- Insert Row Count
  v_exist     number;
  v_value     varchar2(20);
  v_todate    date;
  e_not_completed exception;
  er_point  varchar2(50);
  er_row    number;
  er_msg    varchar2(1000);
  er_msg1   varchar2(1000);
  v_nowdate varchar2(8);
  v_stdate  date;

begin

  -- format : to_system_id|name|๊ตฌ๋ถ„|์šด์šฉํ˜„ํ™ฉ|์‚ฌ์—…๊ตฌ๋ถ„|list_id|๋ณธ๋ถ€||๊ด‘์—ญ์‹œ๋„|์„ธ๋ถ€์ฃผ์†Œ||acc_msc|acc_bsc|acc_list|์žฅ๋น„๋ช…|ํ˜•์ƒ|์œ„๋„(๋„)|์œ„๋„(๋ถ„)|์œ„๋„(์ดˆ)|๊ฒฝ๋„(๋„)|๊ฒฝ๋„(๋ถ„)|๊ฒฝ๋„(์ดˆ)|๊ฐœํ†ต์›”|๊ด€๋ฆฌ๋ฒˆํ˜ธ|seq|CITY|TOWN|VILLAGE|๋…„๋„์ฐจ์ˆ˜
  -- to_system์— ์‹œ์„คํ˜„ํ™ฉ ๋ฐ์ดํ„ฐ๋ฅผ ๋„˜๊ฒจ์ฃผ๊ธฐ ์œ„ํ•œ file์„ ๋งŒ๋“ค์–ด์ค€๋‹ค.
  -- ์œ„์˜ ํฌ๋ฉง์ค‘ ๊ด€๋ฆฌ๋ฒˆํ˜ธ ๋‹ค์Œ์ปฌ๋Ÿผ์œผ๋กœ Seq ๊ฐ€ ์ถ”๊ฐ€๋จ.
  -------------------------------------------------------------------------------------------------------

  v_stdate := sysdate;

  er_point := 'ftp file make as to_system data : list info';
  er_msg   := 'ํ˜„ํ™ฉ์„ ๋งŒ๋“œ๋Š”๋ฐ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜์˜€์Šต๋‹ˆ๋‹ค.';
  ----------------------------------------------------------------------------------------------
  -- ๊ธฐ์ง€๊ตญ ์‹œ์„คํ˜„ํ™ฉ ๋ฐ์ดํ„ฐ๋ฅผ ๋งŒ๋“ ๋‹ค.
  v_filename := 'from_sytstem_to_to_system_list_info.txt';
  -- 2006.6.1 to_system ์œค์˜์‹ ๋Œ€๋ฆฌ์˜ ์š”์ฒญ์— ๋”ฐ๋ผ
  -- ๋งจ ๋’ค์— ์žฅ๋น„๋ช…๊ณผ ํ˜•์ƒ์„ ์ถ”๊ฐ€ํ•˜์˜€์Œ
  ----------------------------------------------------------------------------------------------

  file_handle := sys.utl_file.fopen('DIR1', v_filename, 'w');
  v_cnt       := 0;
  for bb in (select t.to_system_id || '|' || vw.name || '|' || decode(vw.kind, '์ผ๋ฐ˜', '๊ธฐ์ง€๊ตญ', vw.kind) || '|' ||
                     vw.to_system_use || '|' || vw.business || '|' ||
                     t.list_id || '|' || vw.area_id || '|' || '' || '|' || substr(vw.city, 1, 2) || '|' ||
                     vw.address || '|' || '' || '|' || vw.acc_msc || '|' || vw.acc_bsc || '|' ||
                     vw.acc_list || '|' || vw.equip_id || '|' || vw.cfg_type
                     || '|' || vw.latitude1 || '|' || vw.latitude2 || '|' || vw.latitude3 || '|' ||
                     vw.longitude1 || '|' || vw.longitude2 || '|' || vw.longitude3 || '|' ||
                     to_char(t.open_Date, 'YYYYMMDD') || '|' || substr(t.coms_id, 1, 13)
                     || '|' ||
                     decode(length(t.coms_id), 0, '', 13, '01', 16, substr(t.coms_id, 15, 2), '') || '|' ||
                     substr(vw.city, 1, 2) || '|' || vw.town || '|' || vw.village
                     || '|' ||
                     decode(t.year, '', '', substr(t.year, 3, 2) || '๋…„' || to_number(t.seq) || '์ฐจ')
                     || '|' || vw.hz_kind
                     || '|' || t.site_id
                     || '|' || t.project_id
                     || '|' || t.ROAD_NAME
                     || '|' || t.UNDERGROUND
                     || '|' || t.BUILDING_NUM_MAIN
                     || '|' || t.BUILDING_NUM_SUB
                     || '|' || t.BUILDING_NAME
                     || '|' || t.ADDRESS_DONG
                     || '|' || t.ADDRESS_FLOOR
                     || '|' || t.ADDRESS_HO
                     vline
               from from_sytstem_01_list_info_tb t, from_sytstem_01_to_system_info_vw vw
              where vw.info_kind = '๊ธฐ์ง€๊ตญ'
                and t.to_system_id = vw.to_system_id
             
             ) loop
  
    -- Line Text Read
    sys.utl_file.put_line(file_handle, bb.vline);
  
    -- Line Count
    v_cnt := v_cnt + 1;
    -- Line ๋ณ€์ˆ˜ Clear
    v_line := '';
  
  end loop;

  select to_char(sysdate, 'YYYYMMDD') || trim(to_char(v_cnt, '000000')) into v_line from dual;

  sys.utl_file.put_line(file_handle, v_line); -- ๋งจ ๋งˆ์ง€๋ง‰์—๋Š” ์ƒ์„ฑ์ผ์ž์™€ ์ด๋ผ์ธ์ˆ˜๋กœ YYYYMMDDNNNNNN

  -- File CLose
  sys.utl_file.fclose(file_handle);
  update from_sytstem_z99_interface_tb
     set (process_date, process_time) =
          (select to_char(sysdate, 'YYYYMMDD'), to_char(sysdate, 'hh:mi:ss') from dual)
   where interface_name = 'from_sytstem_to_to_system_list_info.txt';

  commit;

  p_app_code := 0;
  p_app_msg  := 'ftp file output complete to to_system !';
exception
  when e_not_completed then
    if sys.utl_file.is_open(file_handle) then
      sys.utl_file.fclose(file_handle);
    end if;
    rollback;
  
  when others then
    rollback;
    if sys.utl_file.is_open(file_handle) then
      sys.utl_file.fclose(file_handle);
    end if;
    p_app_code := sqlcode;
    p_app_msg  := substr(sqlerrm, 1, 200);
  
end prc_file_write_pr;