【UTL_FILE】使用UTL_FILE包生成檔案並寫入資料
使用UTL_FILE包可以實現資料庫向檔案寫入資料的功能。本文給出寫出檔案的基本方法。
1.建立檔案存放的目錄
ora10g@secdb /home/oracle$ mkdir secooler
ora10g@secdb /home/oracle$ cd secooler/
ora10g@secdb /home/oracle/secooler$ ls -ltr
total 0
2.在資料庫中建立DIRECTORY
ora10g@secdb /home/oracle/secooler$ sqlplus / as sysdba
SQL*Plus: Release 10.2.0.1.0 - Production on Wed Jun 15 21:06:09 2011
Copyright (c) 1982, 2005, Oracle. All rights reserved.
Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options
sys@ora10g> create or replace directory dir_secooler as '/home/oracle/secooler';
Directory created.
sys@ora10g> grant read, write on directory dir_secooler to public;
Grant succeeded.
此時dir_secooler建立成功,指向目錄“/home/oracle/secooler”,並且任何使用者都可以使用這個DIRECTORY。
3.使用UTL_FILE包向檔案中寫入資訊
1)連線到sec使用者
sys@ora10g> conn sec/sec
Connected.
2)準備PL/SQL
declare
v_f1 utl_file.file_type;
begin
v_f1 := utl_file.fopen('DIR_SECOOLER','secooler.dat','w');
UTL_FILE.PUT_LINE(v_f1,'Secooler writes this message.');
utl_file.fclose(v_f1);
end;
/
3)執行PL/SQL
sec@ora10g> declare
2 v_f1 utl_file.file_type;
3 begin
4 v_f1 := utl_file.fopen('DIR_SECOOLER','secooler.dat','w');
5 UTL_FILE.PUT_LINE(v_f1,'Secooler writes this message.');
6 utl_file.fclose(v_f1);
7 end;
8 /
PL/SQL procedure successfully completed.
4)檢查執行結果
ora10g@secdb /home/oracle/secooler$ ls -ltr
total 4.0K
-rw-r--r-- 1 oracle oinstall 30 Jun 15 21:58 secooler.dat
ora10g@secdb /home/oracle/secooler$ cat secooler.dat
Secooler writes this message.
可見,secooler.dat檔案已經建立成功,同時資料已經寫入。
4.使用UTL_FILE包建立問題
注意在使用UTL_FILE包用到DIRECTORY資料庫物件時,名字一定要大寫,否則會遭遇“ORA-29280: invalid directory path”錯誤。
sec@ora10g> declare
2 v_f1 utl_file.file_type;
3 begin
4 v_f1 := utl_file.fopen('dir_secooler','secooler.dat','w');
5 UTL_FILE.PUT_LINE(v_f1,'Secooler writes this message.');
6 utl_file.fclose(v_f1);
7 end;
8 /
declare
*
ERROR at line 1:
ORA-29280: invalid directory path
ORA-06512: at "SYS.UTL_FILE", line 33
ORA-06512: at "SYS.UTL_FILE", line 436
ORA-06512: at line 4
5.關於UTL_FILE包參考資訊
官方文件參考連結:
http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14258/u_file.htm#BABDEJDH
6.小結
本文以實現使用UTL_FILE包生成檔案並寫入資訊的簡單功能為例展示了UTL_FILE包的魅力。以此為基礎可以實現更多有價值的需求。
Good luck.
secooler
11.06.15
-- The End --
1.建立檔案存放的目錄
ora10g@secdb /home/oracle$ mkdir secooler
ora10g@secdb /home/oracle$ cd secooler/
ora10g@secdb /home/oracle/secooler$ ls -ltr
total 0
2.在資料庫中建立DIRECTORY
ora10g@secdb /home/oracle/secooler$ sqlplus / as sysdba
SQL*Plus: Release 10.2.0.1.0 - Production on Wed Jun 15 21:06:09 2011
Copyright (c) 1982, 2005, Oracle. All rights reserved.
Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options
sys@ora10g> create or replace directory dir_secooler as '/home/oracle/secooler';
Directory created.
sys@ora10g> grant read, write on directory dir_secooler to public;
Grant succeeded.
此時dir_secooler建立成功,指向目錄“/home/oracle/secooler”,並且任何使用者都可以使用這個DIRECTORY。
3.使用UTL_FILE包向檔案中寫入資訊
1)連線到sec使用者
sys@ora10g> conn sec/sec
Connected.
2)準備PL/SQL
declare
v_f1 utl_file.file_type;
begin
v_f1 := utl_file.fopen('DIR_SECOOLER','secooler.dat','w');
UTL_FILE.PUT_LINE(v_f1,'Secooler writes this message.');
utl_file.fclose(v_f1);
end;
/
3)執行PL/SQL
sec@ora10g> declare
2 v_f1 utl_file.file_type;
3 begin
4 v_f1 := utl_file.fopen('DIR_SECOOLER','secooler.dat','w');
5 UTL_FILE.PUT_LINE(v_f1,'Secooler writes this message.');
6 utl_file.fclose(v_f1);
7 end;
8 /
PL/SQL procedure successfully completed.
4)檢查執行結果
ora10g@secdb /home/oracle/secooler$ ls -ltr
total 4.0K
-rw-r--r-- 1 oracle oinstall 30 Jun 15 21:58 secooler.dat
ora10g@secdb /home/oracle/secooler$ cat secooler.dat
Secooler writes this message.
可見,secooler.dat檔案已經建立成功,同時資料已經寫入。
4.使用UTL_FILE包建立問題
注意在使用UTL_FILE包用到DIRECTORY資料庫物件時,名字一定要大寫,否則會遭遇“ORA-29280: invalid directory path”錯誤。
sec@ora10g> declare
2 v_f1 utl_file.file_type;
3 begin
4 v_f1 := utl_file.fopen('dir_secooler','secooler.dat','w');
5 UTL_FILE.PUT_LINE(v_f1,'Secooler writes this message.');
6 utl_file.fclose(v_f1);
7 end;
8 /
declare
*
ERROR at line 1:
ORA-29280: invalid directory path
ORA-06512: at "SYS.UTL_FILE", line 33
ORA-06512: at "SYS.UTL_FILE", line 436
ORA-06512: at line 4
5.關於UTL_FILE包參考資訊
官方文件參考連結:
http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14258/u_file.htm#BABDEJDH
6.小結
本文以實現使用UTL_FILE包生成檔案並寫入資訊的簡單功能為例展示了UTL_FILE包的魅力。以此為基礎可以實現更多有價值的需求。
Good luck.
secooler
11.06.15
-- The End --
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/519536/viewspace-700095/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 讀取檔案流並寫入檔案流
- java資料list寫入檔案Java
- 用 ABAP 新建本地 Excel 檔案並寫入資料試讀版Excel
- iOS自定義log並寫入檔案iOS
- python如何開啟帶變數名的txt檔案並寫入資料Python變數
- 使用pd從資料庫逆向生成pdm檔案資料庫
- 使用Busybox製作最小檔案系統並燒寫入開發板
- Laravel 遷移檔案生成包Laravel
- 從cmd中匯入.SQL檔案並建立資料庫SQL資料庫
- python——將excel檔案寫入mysql資料庫中PythonExcelMySql資料庫
- 【Python】將網格資料寫入到VTK檔案Python
- python如何將資料寫入本地txt文字檔案Python
- (資料科學學習手札59)從抓取資料到生成shp檔案並展示資料科學
- C語言產生正弦波,將資料寫入檔案中並用gnuplot作圖C語言
- php百萬資料透過指令碼檔案寫入csvPHP指令碼
- PHP寫入檔案PHP
- 【小專案】爬取上海票據交易所資料並寫入資料庫資料庫
- Linux-檔案寫入和檔案同步Linux
- python 寫入CSV檔案Python
- 追加寫入json檔案JSON
- 獲取 CPU資訊,並透過登錄檔寫入
- Java 渲染 docx 檔案,並生成 pdf 加水印Java
- 使用 SAP ABAP 程式碼生成 PDF 檔案,填充以業務資料並顯示在 SAPGUI 裡試讀版GUI
- abp框架寫實體類並生成對應的資料庫框架資料庫
- Spring Boot入門(一):使用IDEA建立Spring Boot專案並使用yaml配置檔案Spring BootIdeaYAML
- 無法寫入檔案 Filesystem
- python---json檔案寫入PythonJSON
- 圖片寫入pdf檔案
- Java 例項 - 檔案寫入Java
- 【深入】體系結構-002-資料變化如何寫入檔案
- wireshark使用-----拆分抓包檔案
- Java 匯入資料到Excel並提供檔案下載介面JavaExcel
- Mysql資料庫使用Navicat Mysql匯入sql檔案報錯MySql資料庫
- golang寫入檔案時,覆蓋前檔案(將前檔案清空)Golang
- 使用yaml檔案讀取資料YAML
- 使用stub快速生成自定義檔案
- coredump檔案生成,以及GDB工具使用
- 獲取一個資料夾下所有指定字尾名(.java)的檔案(包括子資料夾中的內容),並將這些檔案的絕對路徑寫入到一個文字檔案中Java
- pytesseract實現識別pdf檔案並將內容寫入word文件中