利用存储过程数据库中的数据生成txt文件

 PROCEDURE PROC_WRITE_DAT_POD(  o_sts  OUT BINARY_INTEGER,
                           o_errm  OUT VARCHAR2)

IS

 PROCESS_ERROR  EXCEPTION;
 w_errm   VARCHAR2(100); 
 w_cursor  T_CURSOR;
 l_dat_file  utl_file.file_type;
  w_sql1   VARCHAR2(4000 BYTE); 
 
 w_inp_str  VARCHAR2(4000);  
 w_file_name  VARCHAR2(200);          --ファイル名


 w_create_folder_sql VARCHAR2(200);
 w_fexist_flg  BOOLEAN;
 
 w_block_size  BINARY_INTEGER;
 
  w_cis_ms_item CIS_MS_ITEM1;
  w_flength  NUMBER:=0;
  c_gst_filepath  VARCHAR2(20) := 'UCE_DIR';
  w_path VARCHAR2(20) := 'E:/ztg';
 
BEGIN
 o_sts := c_process_sts_ok;
 --フォルダを指定する
  w_create_folder_sql :=' create or replace directory '||c_gst_filepath|| ' as '''
                    || w_path ||'''';
 EXECUTE IMMEDIATE w_create_folder_sql;
 
  w_file_name:='test.txt';
  --ファイルを生成する
 l_dat_file :=utl_file.fopen(c_gst_filepath,w_file_name ,'w',32767); 
 utl_file.fgetattr(c_gst_filepath,w_file_name,w_fexist_flg,w_flength,w_block_size);
 IF NOT w_fexist_flg THEN
  w_errm :='Create file error.';
  RAISE PROCESS_ERROR;
 END IF;
 
  --1:Segment Name:  Message Header部分を記入する
  w_inp_str :=  'Hear is message header!!!!!!!!!!!';
   IF utl_file.is_open(l_dat_file) THEN
      utl_file.put_line(l_dat_file,w_inp_str,TRUE);
      --w_flength := w_flength+1;
   ELSE
      w_errm :='Write  file error';
      RAISE PROCESS_ERROR;   
   END IF;
 
  --w_inp_strをクリアする
  w_inp_str :='';
 
  --2:Segment Name:  Header Information部分を記入する
 w_sql1:=' select item_code,vendor_code,item_description,ins_date from cis_ms_item t ';
  --|| ' where t.item_code = ''' || i_vmisitecode || '''';

  --w_sql1を実行する
  OPEN w_cursor FOR w_sql1;
 LOOP
     FETCH w_cursor INTO w_cis_ms_item;
      EXIT WHEN w_cursor%NOTFOUND;
      --ファイルにデータを記入する
      w_inp_str := w_cis_ms_item.item_code || ','
                  || w_cis_ms_item.vendor_code || ','
                  || w_cis_ms_item.item_description || ','
                  || w_cis_ms_item.ins_date;
      IF utl_file.is_open(l_dat_file) THEN
          utl_file.put_line(l_dat_file,w_inp_str,TRUE);
          w_flength := w_flength+1;
      ELSE
          w_errm :='Write  file error';
          RAISE PROCESS_ERROR;       
      END IF;
  END LOOP;
  CLOSE w_cursor;
  --w_inp_strをクリアする
  w_inp_str :='';
  -- 4:Segment Name:  Message Trailer部分を記入する
  w_inp_str :=  'レコード合計数:' || w_flength;
   IF utl_file.is_open(l_dat_file) THEN
      utl_file.put_line(l_dat_file,w_inp_str,TRUE);
   ELSE
      w_errm :='Write  file error';
      RAISE PROCESS_ERROR;      
  END IF;
 utl_file.fclose_all;
EXCEPTION
 WHEN PROCESS_ERROR THEN
  ROLLBACK;
    utl_file.fclose_all;
    --生成したファイルを削除する
  utl_file.fgetattr(c_gst_filepath,w_file_name,w_fexist_flg,w_flength,w_block_size);
  IF w_fexist_flg THEN
   utl_file.fremove(c_gst_filepath,w_file_name);
  END IF;
    o_sts:=c_process_sts_error;
    o_errm :=dbms_utility.format_error_stack;
 WHEN OTHERS THEN
  ROLLBACK;
    utl_file.fclose_all;
  --生成したファイルを削除する
  utl_file.fgetattr(c_gst_filepath,w_file_name,w_fexist_flg,w_flength,w_block_size);
  IF w_fexist_flg THEN
   utl_file.fremove(c_gst_filepath,w_file_name);
  END IF;
    o_sts:=c_process_sts_error;
    o_errm :=dbms_utility.format_error_stack;
END;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值