waterye

使用utl_file包进行io操作

1. 首先要创建目录
CREATE DIRECTORY log_dir AS 'd:/ora_log';

2. 写log
DECLARE
  p_dir       
varchar2(100);
  p_filename  
varchar2(100);
  output_file utl_file.file_type;
begin
  p_dir       :
= 'log_dir';
  p_filename  :
= 'log_' || to_char(sysdate, 'yyyy_mm_dd_HH24_MI_SS'|| '.txt';
  output_file :
= utl_file.fopen(upper(p_dir), p_filename, 'w');
  
delete from test;
  utl_file.put(output_file, 'test:  ' || SQL%ROWCOUNT || ' rows deleted.');

  utl_file.new_line(output_file);
  
insert into test
    
select * from test@remotedb;
  utl_file.put(output_file, 
'test:  ' || SQL%ROWCOUNT || ' rows inserted.');
  utl_file.new_line(output_file);
  utl_file.fclose(output_file);
  
commit;
EXCEPTION
  
WHEN OTHERS THEN
      utl_file.put(output_file, 'error: ' || to_char(sysdate, 'yyyy-mm-dd HH24:MI:SS'));
      utl_file.new_line(output_file);
      utl_file.put(output_file, 'SQLCODE:'||SQLCODE);
      utl_file.new_line(output_file);
      utl_file.put(output_file, 'SQLERRM:'||SQLERRM);
      utl_file.new_line(output_file);
      utl_file.fclose(output_file);   
    
ROLLBACK;
end;
/

参考:
1. PL/SQL Packages and Types Reference
2. AskTom
3. Itpub

posted on 2005-12-31 17:57 waterye 阅读(851) 评论(0)  编辑  收藏 所属分类: oracle


只有注册用户登录后才能发表评论。


网站导航: