经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » Oracle » 查看文章
Oracle的创建文件
来源:cnblogs  作者:DarkerGuo  时间:2021/5/17 9:17:42  对本文有异议

1.创建目录(需要在服务器上创建对应的目录)
create or replace directory testdir as '/u01/software/test';
2.给用户授权(这里可能会有问题,找不到xxx用户,需要创建用户)
grant read,write on directory testdir to xxx;
3.写入文件脚本

  1. declare
  2. filehandle utl_file.file_type; --句柄
  3. begin
  4. filehandle := utl_file.fopen('TESTDIR', 'hello.txt', 'w'); --打开文件
  5. utl_file.put_line(filehandle, 'Hello World!');
  6. utl_file.fclose(filehandle); --关闭句柄
  7. end;

暂存另一份脚本

  1. create or replace procedure get_test_blob(i_id varchar2) is
  2. l_file utl_file.file_type;
  3. l_filename varchar2(300);
  4. v_modules varchar2(3000);
  5. v_classes varchar2(3000);
  6. v_proc varchar2(3000);
  7. v_sysdate varchar2(3000);
  8. v_count number;
  9. begin
  10. l_filename := to_char(sysdate, 'yyyymmdd') || i_id ||'.sh';
  11. l_file := utl_file.fopen('TESTDIR', l_filename, 'w');
  12. dbms_output.put_line('===OPEN OK===' || l_filename);
  13. dbms_output.put_line('#!/bin/sh');
  14. utl_file.put_line(l_file, '#!/bin/sh'); --写入文件
  15. v_sysdate := to_char(sysdate, 'yyyymmdd') || i_id;
  16. v_modules := '/u01/srm/prod/' || v_sysdate || '/webapp/webRoot/';
  17. v_classes := '/u01/srm/prod/' || v_sysdate ||
  18. '/webapp/webRoot/WEB-INF/classes/';
  19. v_proc := '/u01/srm/prod/' || v_sysdate || '/jobRunner/';
  20. dbms_output.put_line('mkdir -p /u01/srm/prod/' || v_sysdate ||
  21. '/webapp/webRoot/modules');
  22. dbms_output.put_line('mkdir -p /u01/srm/prod/' || v_sysdate ||
  23. '/webapp/webRoot/WEB-INF/classes');
  24. utl_file.put_line(l_file,
  25. 'mkdir -p /u01/srm/prod/' || v_sysdate ||
  26. '/webapp/webRoot/modules');
  27. utl_file.put_line(l_file,
  28. 'mkdir -p /u01/srm/prod/' || v_sysdate ||
  29. '/webapp/webRoot/WEB-INF/classes');
  30. select count(*) into v_count from h3c_test_pro_proc;
  31. if v_count > 0 then
  32. dbms_output.put_line('mkdir -p /u01/srm/prod/' || v_sysdate ||
  33. '/jobRunner');
  34. utl_file.put_line(l_file,
  35. 'mkdir -p /u01/srm/prod/' || v_sysdate ||
  36. '/jobRunner');
  37. end if;
  38. dbms_output.put_line('#screen');
  39. utl_file.put_line(l_file, '#screen');
  40. for rec in (select distinct trim(h.screen_path) screen_path
  41. from h3c_test_pro_screen h) loop
  42. --复制某个目录下的文件
  43. if rec.screen_path like '%.screen' or rec.screen_path like '%.svc' then
  44. dbms_output.put_line('mkdir -p ' || v_modules ||
  45. substr(rec.screen_path,
  46. 1,
  47. instr(rec.screen_path,
  48. '/',
  49. 1,
  50. length(rec.screen_path) -
  51. length(replace(rec.screen_path,
  52. '/',
  53. '')))));
  54. dbms_output.put_line('cp -rn /u01/srm/webapp/webRoot/' ||
  55. rec.screen_path || ' ' || v_modules ||
  56. rec.screen_path);
  57. utl_file.put_line(l_file,
  58. 'mkdir -p ' || v_modules ||
  59. substr(rec.screen_path,
  60. 1,
  61. instr(rec.screen_path,
  62. '/',
  63. 1,
  64. length(rec.screen_path) -
  65. length(replace(rec.screen_path, '/', '')))));
  66. utl_file.put_line(l_file,
  67. 'cp -rn /u01/srm/webapp/webRoot/' ||
  68. rec.screen_path || ' ' || v_modules ||
  69. rec.screen_path);
  70. --复制文件夹
  71. else
  72. dbms_output.put_line('mkdir -p ' || v_modules || rec.screen_path);
  73. dbms_output.put_line('cp -rn /u01/srm/webapp/webRoot/' ||
  74. rec.screen_path || ' ' || v_modules ||
  75. substr(rec.screen_path,
  76. 1,
  77. instr(rec.screen_path,
  78. '/',
  79. 1,
  80. length(rec.screen_path) -
  81. length(replace(rec.screen_path,
  82. '/',
  83. '')) - 1)));
  84. utl_file.put_line(l_file,
  85. 'mkdir -p ' || v_modules || rec.screen_path);
  86. utl_file.put_line(l_file,
  87. 'cp -rn /u01/srm/webapp/webRoot/' ||
  88. rec.screen_path || ' ' || v_modules ||
  89. substr(rec.screen_path,
  90. 1,
  91. instr(rec.screen_path,
  92. '/',
  93. 1,
  94. length(rec.screen_path) -
  95. length(replace(rec.screen_path, '/', '')) - 1)));
  96. end if;
  97. end loop;
  98. dbms_output.put_line('#bm');
  99. utl_file.put_line(l_file, '#bm');
  100. for rec in (select distinct trim(h.bm_path) bm_path from h3c_test_pro_bm h) loop
  101. if rec.bm_path like '%.bm' then
  102. dbms_output.put_line('mkdir -p ' || v_classes ||
  103. replace(substr(rec.bm_path,
  104. 1,
  105. instr(replace(rec.bm_path,
  106. '.bm',
  107. ''),
  108. '.',
  109. -1) - 1),
  110. '.',
  111. '/'));
  112. dbms_output.put_line('cp -rn /u01/srm/webapp/webRoot/WEB-INF/classes/' ||
  113. replace(replace(rec.bm_path, '.bm', ''),
  114. '.',
  115. '/') || '.bm' || ' ' || v_classes ||
  116. replace(replace(rec.bm_path, '.bm', ''),
  117. '.',
  118. '/') || '.bm');
  119. utl_file.put_line(l_file,
  120. 'mkdir -p ' || v_classes ||
  121. replace(substr(rec.bm_path,
  122. 1,
  123. instr(replace(rec.bm_path, '.bm', ''),
  124. '.',
  125. -1) - 1),
  126. '.',
  127. '/'));
  128. utl_file.put_line(l_file,
  129. 'cp -rn /u01/srm/webapp/webRoot/WEB-INF/classes/' ||
  130. replace(replace(rec.bm_path, '.bm', ''), '.', '/') ||
  131. '.bm' || ' ' || v_classes ||
  132. replace(replace(rec.bm_path, '.bm', ''), '.', '/') ||
  133. '.bm');
  134. else
  135. dbms_output.put_line('mkdir -p ' || v_classes ||
  136. replace(rec.bm_path, '.', '/'));
  137. dbms_output.put_line('cp -rn /u01/srm/webapp/webRoot/WEB-INF/classes/' ||
  138. replace(rec.bm_path, '.', '/') || ' ' ||
  139. v_classes ||
  140. substr(rec.bm_path,
  141. 1,
  142. instr(rec.bm_path, '.', 1) - 1));
  143. utl_file.put_line(l_file,
  144. 'mkdir -p ' || v_classes ||
  145. replace(rec.bm_path, '.', '/'));
  146. utl_file.put_line(l_file,
  147. 'cp -rn /u01/srm/webapp/webRoot/WEB-INF/classes/' ||
  148. replace(rec.bm_path, '.', '/') || ' ' || v_classes ||
  149. substr(rec.bm_path,
  150. 1,
  151. instr(rec.bm_path, '.', 1) - 1));
  152. end if;
  153. end loop;
  154. dbms_output.put_line('#proc');
  155. utl_file.put_line(l_file, '#proc');
  156. for rec in (select distinct trim(h.bm_path) proc_path
  157. from h3c_test_pro_proc h) loop
  158. if rec.proc_path like '%.bm' or rec.proc_path like '%.proc' then
  159. dbms_output.put_line('mkdir -p ' || v_proc ||
  160. substr(rec.proc_path,
  161. 1,
  162. instr(rec.proc_path,
  163. '/',
  164. 1,
  165. length(rec.proc_path) -
  166. length(replace(rec.proc_path,
  167. '/',
  168. '')))));
  169. dbms_output.put_line('cp -rn /u01/srm/jobRunner/' || rec.proc_path || ' ' ||
  170. v_proc || rec.proc_path);
  171. utl_file.put_line(l_file,
  172. 'mkdir -p ' || v_proc ||
  173. substr(rec.proc_path,
  174. 1,
  175. instr(rec.proc_path,
  176. '/',
  177. 1,
  178. length(rec.proc_path) -
  179. length(replace(rec.proc_path, '/', '')))));
  180. utl_file.put_line(l_file,
  181. 'cp -rn /u01/srm/jobRunner/' || rec.proc_path || ' ' ||
  182. v_proc || rec.proc_path);
  183. end if;
  184. end loop;
  185. dbms_output.put_line('===EXPORT OK===');
  186. utl_file.fclose(l_file);
  187. exception
  188. when utl_file.invalid_path then
  189. --无效的路径
  190. dbms_output.put_line('===INVALID_PATH===' || i_id);
  191. raise;
  192. when utl_file.invalid_mode then
  193. --无效的打开模式
  194. dbms_output.put_line('===INVALID_MODE===' || i_id);
  195. raise;
  196. when utl_file.invalid_operation then
  197. --无效的操作,文件打开错误会报这个异常,一般来说都是超长或打开方式byte型和非byte
  198. dbms_output.put_line('===INVALID_OPERATION===' || i_id);
  199. raise;
  200. when utl_file.invalid_maxlinesize then
  201. --无效的最大长度,varchar2最大4000raw最大32676,超过回报这个异常,所以一般要进行循环操作
  202. dbms_output.put_line('===INVALID_MAXLINESIZE===' || i_id);
  203. raise;
  204. when utl_file.access_denied then
  205. --拒绝进入指定路径,可能是授权问题
  206. dbms_output.put_line('===ACCESS_DENIED===' || i_id);
  207. raise;
  208. when utl_file.invalid_filehandle then
  209. --文件处理错误,不常见
  210. dbms_output.put_line('===INVALID_FILEHANDLE===' || i_id);
  211. raise;
  212. when utl_file.write_error then
  213. --写入错误,处理该异常最好的方式是将要写入的文件简单化,然后找准错误原因
  214. dbms_output.put_line('===WRITE_ERROR===' || i_id);
  215. raise;
  216. when no_data_found then
  217. --select时候未找到数据,不是utl_file的异常
  218. dbms_output.put_line('===NO_DATA_FOUND===' || i_id);
  219. utl_file.fclose(l_file);
  220. raise;
  221. when others then
  222. if utl_file.is_open(l_file) then
  223. utl_file.fclose(l_file);
  224. raise;
  225. end if;
  226. end get_test_blob;

原文链接:http://www.cnblogs.com/darkerguo/p/14772696.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号