Oracle笔记 十四、查询XML操作、操作系统文件

--1.随机数
select dbms_random.value from dual;
select mod(dbms_random.random, 10) from dual;
--0-9随机数
select abs(mod(dbms_random.random, 10)) from dual;
--40-49随机数
select 40 + abs(mod(dbms_random.random, 10)) from dual;
 
--2.Xml
declare
       words clob;
       xmlStr varchar2(32767);
       line varchar2(2000);
       line_no number := 1;
begin
     words := dbms_xmlquery.getXML('select * from scott.emp');
     xmlStr := dbms_lob.substr(words, 32767);
     loop
         exit when (xmlStr is null);
         line := substr(xmlStr, 1, instr(xmlStr, chr(10)) - 1);
         dbms_output.put_line(line_no || ':' || line);
         xmlStr := substr(xmlStr, instr(xmlStr, chr(10)) + 1);
         line_no := line_no + 1;
     end loop;
end;
 
--3.文件
--定义文件夹 命名必须大写
create directory MY_DIR as 'D:/TEMP';
--读文件
declare
       inputfile UTL_FILE.file_type; --文件对象
       input varchar2(2000);
begin
     --指定文件
     --3个参数依次为:文件夹 文件 打开方式[r(读) w(写) a(追加)]
     inputfile := UTL_FILE.fopen('MY_DIR', 'demo.txt', 'r');
     loop              
         UTL_FILE.get_line(inputfile, input);
         dbms_output.put_line(input);             
     end loop;
     --关闭文件
     UTL_FILE.fclose(inputfile);
     exception
       when no_data_found then dbms_output.put_line('文件末尾!');
end;
 
--写文件
declare
       inputfile UTL_FILE.file_type; --文件对象
       input varchar2(2000) := 'Hello World!';
begin
     --指定文件
     --3个参数依次为:文件夹 文件 打开方式[r(读) w(写) a(追加)]
     inputfile := UTL_FILE.fopen('MY_DIR', 'mydemo.txt', 'a');
     --写入数据
     UTL_FILE.put_line(inputfile, input);
     --关闭文件
     UTL_FILE.fclose(inputfile);
     exception
       when no_data_found then dbms_output.put_line('文件末尾!');
end;
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值