绑定完请刷新页面
取消
刷新

分享好友

×
取消 复制
包-UTL_FILE
2020-01-16 17:42:16

--系统管理员向PG_DIRECTORY系统表中加入目录/temp/:
CREATE OR REPLACE DIRECTORY dir AS '/tmp/';
--打开一个文件并向文件中写入数据                                                                                                                   
DECLARE
  f utl_file.file_type;
  dir text := 'dir';
BEGIN
  f := utl_file.fopen(dir, 'sample.txt', 'w');
  PERFORM utl_file.put_line(f, 'ABC');
  PERFORM utl_file.put_line(f, '123'::numeric);
  PERFORM utl_file.put_line(f, '-----');
  PERFORM utl_file.new_line(f);
  PERFORM utl_file.put_line(f, '*******');
  PERFORM utl_file.new_line(f, 0);
  PERFORM utl_file.put_line(f, '++++++++');
  PERFORM utl_file.new_line(f, 2);
  PERFORM utl_file.put_line(f, '#######');
  PERFORM utl_file.put(f, 'A');
  PERFORM utl_file.put(f, 'B');
  PERFORM utl_file.new_line(f);
  PERFORM utl_file.putf(f, '[1 -> %s, 2 -> %s, 3 -> %s, 4 -> %s, 5 -> %s]', 'gaussdb', 'utl', 'file', 'get', 'line');
  PERFORM utl_file.new_line(f);
  PERFORM utl_file.put_line(f, '1234567890');
  f := utl_file.fclose(f);
END;
/
--在上面写入的文件中读取数据。
DECLARE
  f utl_file.file_type;
  dir text := 'dir';
BEGIN
  f := utl_file.fopen(dir, 'sample.txt', 'r');
  FOR i IN 1..11 LOOP
       RAISE INFO '[%] : %', i, utl_file.get_line(f);
  END LOOP;
END;
/
-- 对文件句柄执行位置偏移,并获取文件的当前位置。                                                                                                 
DECLARE
      l_file   utl_file.file_type;
      l_buffer VARCHAR2(32767);
      dir text := 'dir';
      abs_offset number := 100;
      rel_offset number := NULL;
BEGIN
      l_file := utl_file.fopen(location  => dir,  filename  => 'sample.txt',open_mode => 'R');
      dbms_output.put_line('before fseek: current position is ' || utl_file.fgetpos(file => l_file)); -- before fseek: current position is 0
      utl_file.fseek(file => l_file, absolute_offset=>abs_offset, relative_offset=>rel_offset);
      dbms_output.put_line('fseek: current position is ' || utl_file.fgetpos(file => l_file)); -- fseek: current position is 100
      utl_file.fclose(file=>l_file);
END;
/

分享好友

分享这个小栈给你的朋友们,一起进步吧。

GaussDB_数据库
创建时间:2020-01-06 16:21:44
华为GaussDB数据库小栈
展开
订阅须知

• 所有用户可根据关注领域订阅专区或所有专区

• 付费订阅:虚拟交易,一经交易不退款;若特殊情况,可3日内客服咨询

• 专区发布评论属默认订阅所评论专区(除付费小栈外)

技术专家

查看更多
  • GaussDB_数据库
    专家
戳我,来吐槽~