Posted by & filed under ORACLE.

リンク: [Chapter 6] 6.2 UTL_FILE: Reading and Writing Server-side Files.
リンク: Oracle PL/SQL 編7 – 標準ファイル I/O utl_file.fopen、utl_file.put_line – SAK Streets.

■レコードグルーブシーケンス番号を付与して、CSV 形式でテキスファイル出力

set serveroutput on;

declare
  cursor c1 is select 得意先CD, 品番
    from 受注m order by 得意先CD, 品番;
  ct number;
  svtkcd varchar2(20) := ‘ ‘;
  fno utl_file.file_type;
  rec varchar2(1000);
begin
  dbms_output.put_line (’start…’);
  fno := utl_file.fopen(’g:tmp’, ‘test.txt’, ‘W’);
  for c1_rec in c1 loop
    if svtkcd != c1_rec.得意先CD then
      ct := 0;
      svtkcd := c1_rec.得意先CD;
    end if;
    ct := ct + 1;
    rec := ‘”‘ || ct
      || ‘”,”‘ || c1_rec.得意先CD
      || ‘”,”‘ || c1_rec.品番
      || ‘”‘;
    utl_file.put_line(fno, rec);
  end loop;
  utl_file.fclose(fno);
  dbms_output.put_line (’end’);
exception
  when OTHERS then
    dbms_output.put_line (’** エラー **’);
end;
/

Comments are closed.