`
xiaoer_1982
  • 浏览: 1864922 次
  • 性别: Icon_minigender_2
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

Oracle UTL_FILE 用法例子

阅读更多
General Information
Note: O/S permissions are those of the user 'Oracle' ... not the schema owner or connected user
Source {ORACLE_HOME}/rdbms/admin/utlfile.sql
First Availability 7.3.4
init.ora Parameters utl_file_dir
utl_file_dir=c:\oraload
utl_file_dir=c:\temp
utl_file_dir=*
Open Modes
A Append Text
AB Append Byte Mode
R Read Text
RB Read Byte Mode
W Write Text
WB Write Byte Mode
FCLOSE
Close A File Opened By UTL_FILE utl_file.fclose(<file_handle>)
see FOPEN demo
FCLOSE_ALL
Close All Files Opened By UTL_FILE utl_file.fclose_all;
set serveroutput on

DECLARE
vInHandle utl_file.file_type ;
vOutHandle utl_file.file_type ;
BEGIN
vInHandle := utl_file.fopen ('ORALOAD', 'test.txt', 'R');
vOutHandle := utl_file.fopen ('ORALOAD', 'out.txt', 'W');

IF utl_file.is_open (vInHandle) THEN
utl_file.fclose_all ;
dbms_output.put_line('Closed All');
END IF;
END fopen;
/
FCOPY
Copies a contiguous portion of a file to a newly created file utl_file.fcopy (
location IN VARCHAR2,
filename IN VARCHAR2,
dest_dir IN VARCHAR2,
dest_file IN VARCHAR2,
start_line IN PLS_INTEGER DEFAULT 1,
end_line IN PLS_INTEGER DEFAULT NULL);
-- demo requires creating directory CTEMP ... see link at bottom of page

BEGIN
utl_file.fcopy ('ORALOAD', 'dump.txt', 'ORALOAD', 'didit.txt');
END;
/
FFLUSH
Physically writes pending data to the file identified by the file handle utl_file.fflush (<file_handle>);
See Write demo
FGETATTR
Reads and returns the attributes of a disk file utl_file.fgetattr(
location IN VARCHAR2,
filename IN VARCHAR2,
exists OUT BOOLEAN,
file_length OUT NUMBER,
blocksize OUT NUMBER);
set serveroutput on

DECLARE
ex BOOLEAN;
flen NUMBER;
bsize NUMBER;
BEGIN
utl_file.fgetattr ('ORALOAD', 'test.txt', ex, flen, bsize);

IF ex THEN
dbms_output.put_line('File Exists');
ELSE
dbms_output.put_line('File Does Not Exist');
END IF;
dbms_output.put_line('File Length: ' || TO_CHAR(flen));
dbms_output.put_line('Block Size: ' || TO_CHAR(bsize));
END fgetattr;
/
FGETPOS
Returns the current relative offset position within a file, in bytes utl_file.fgetpos(fileid IN file_type) RETURN PLS_INTEGER;
See Read-Write demo
FOPEN
Open A File For Read Operations utl_file.fopen(
<file_location IN VARCHAR2>,
<file_name IN VARCHAR2>,
<open_mode IN VARCHAR2>,
<max_linesize IN BINARY_INTEGER>)
RETURN <file_type_package_data_type;
DECLARE
vInHandle utl_file.file_type ;
vNewLine VARCHAR2(250);
BEGIN
vInHandle := utl_file.fopen ('ORALOAD', 'test.txt', 'R');
LOOP
BEGIN
utl_file.get_line (vInHandle, vNewLine);
dbms_output.put_line(vNewLine);
EXCEPTION
WHEN OTHERS THEN
EXIT;
END;
END LOOP;
utl_file.fclose (vInHandle);
END fopen;
/
Open A File For Write Operations <file_handle> := utl_file.fopen(<file_location, file_name, 'w ')
FOPEN_NCHAR
Open a file to read or write a text file in Unicode instead of in the database charset
FREMOVE
Delete An Operating System File utl_file.fremove (location IN VARCHAR2, filename IN VARCHAR2);
BEGIN
utl_file.fremove ('ORALOAD', 'dump.txt');
END fremove;
/
FRENAME
Rename An Operating System File utl_file.frename (
location IN VARCHAR2,
filename IN VARCHAR2,
dest_dir IN VARCHAR2,
dest_file IN VARCHAR2,
overwrite IN BOOLEAN DEFAULT FALSE);
BEGIN
utl_file.frename ('ORALOAD','test.txt','ORALOAD','x.txt',TRUE);
END frename;
/
FSEEK
Adjusts the file pointer forward or backward within the file by the number of bytes specified utl_file.fseek (
fid IN utl_file.file_type,
absolute_offset IN PL_INTEGER DEFAULT NULL,
relative_offset IN PLS_INTEGER DEFAULT NULL);
See Read-Write demo
GETLINE
Read a Line from a file utl_file.getline (
file IN FILE_TYPE,
buffer OUT VARCHAR2,
linesize IN NUMBER,
len IN PLS_INTEGER DEFAULT NULL);
See Read demos
GETLINE_NCHAR
Same as GETLINE except can be used to read Unicode rather than the database's character set
GET_RAW
Reads a RAW string value from a file and adjusts the file pointer ahead by the number of bytes read utl_file.get_raw (
fid IN utl_file.file_type,
r OUT NOCOPY RAW,
len IN PLS_INTEGER DEFAULT NULL);
See UTL_MAIL demo
IS_OPEN
Returns True If A File Handle Is Open: Otherwise False utl_file.is_open (file IN FILE_TYPE) RETURN BOOLEAN;
See FCLOSE_ALL Demo
NEW_LINE
Writes one or more operating system-specific line terminators to a file utl_file.NEW_LINE (file IN FILE_TYPE, lines IN NATURAL := 1);
See Read Demo
PUT
Writes a string to a file utl_file.put(
file IN FILE_TYPE,
buffer IN VARCHAR2);
See Write demo
PUTF
A PUT procedure with formatting utl_file.putf(
file IN FILE_TYPE,
format IN VARCHAR2,
[arg1 IN VARCHAR2 DEFAULT NULL,
. . .
arg5 IN VARCHAR2 DEFAULT NULL]);
See Write demo
PUT_LINE
Writes a line to a file. Appends an operating system-specific line terminator utl_file.put_line(
file IN FILE_TYPE,
buffer IN VARCHAR2,
autoflush IN BOOLEAN DEFAULT FALSE);
See Read-Write demo
PUT_NCHAR
Writes a Unicode string to a file
PUT_RAW
Accepts as input a RAW data value and writes the value to the output buffer utl_file.PUT_RAW (
fid IN utl_file.file_type,
r IN RAW,
autoflush IN BOOLEAN DEFAULT FALSE);
See extract_blob Demo
PUT_LINE_NCHAR
Writes a Unicode line to a file
PUTF_NCHAR
Writes a Unicode string to a file

本文转自:http://www.psoug.org/reference/utl_file.html

分享到:
评论

相关推荐

    oracle触发器实现二维码

    它使用UTL_FILE包与文件系统交互,生成二维码图片,然后读取该文件的内容并将其转换为BLOB,最后更新表中的二维码图像字段。 "loadjava"标签可能指的是使用Oracle的LOADJAVA工具来加载Java源代码或类文件到数据库,...

    Oracle P/L SQL生成XML格式的Excel内容

    3. 使用Oracle的UTL_FILE包进行文件操作 4. 可能的自定义PL/SQL包"UTL_Excel"的使用和调用 5. 数据查询与转换 6. Excel对XML文件的解析和支持 通过学习和实践这些内容,你将能够有效地从Oracle数据库生成符合Excel...

    Oracle的大对象处理(代码加注释)

    可以使用DBMS_REDEFINITION或其他方法进行表重定义,或者直接删除不再需要的记录。 通过以上介绍,我们可以了解到Oracle的大对象处理涉及数据存储、读取、更新、删除以及性能优化等多个方面。在实际开发中,根据...

    clob的保存和修改

    Oracle中的例子如下: ```sql DECLARE l_clob CLOB; l_file UTL_FILE.FILE_TYPE; BEGIN l_file := UTL_FILE.FOPEN('MY_DIR', 'my_file.txt', 'R'); DBMS_LOB.CREATETEMPORARY(l_clob, TRUE); DBMS_LOB....

    ORACLE 表数据写成TXT文本,CVS等格式存储过程

    本存储过程提供了一种有效的方法,用于将Oracle表数据导出为TXT或CSV格式,并且具备批量删除功能,适用于需要定期处理大量数据的应用场景。通过理解上述代码,我们可以更好地掌握如何利用Oracle的内置函数和存储过程...

    ORACLE中的FTP例子代码

    这篇博客"ORACLE中的FTP例子代码"提供了在Oracle中实现FTP操作的示例代码,对于理解如何在PL/SQL中集成FTP功能非常有帮助。 首先,Oracle数据库自身并不直接支持FTP操作,但可以通过调用操作系统命令或者使用Java...

    oracle调用java包

    使用`DBMS_JAVA.GRANT_PERMISSION`授予权限,然后使用`DBMS_JAVA.PRECOMPILE`预编译Java源代码,最后使用`UTL_FILE.PUT_LINE`或其他方法上传.jar文件到数据库的特定目录对象。 3. 示例: ``` DECLARE jar_file ...

    Oracle 有 选择地导出数据 到 excel的方法

    如果希望得到标准的Excel格式(.xlsx),可以考虑使用Oracle的SQL Developer工具,或者利用PL/SQL的UTL_FILE包配合外部表来达到目的。 总的来说,通过SQL*Plus和适当的脚本,我们可以灵活地从Oracle数据库中选择性...

    一个有趣的东西-cloudeye1

    Oracle数据库中,我们可以使用`UTL_INADDR.GET_HOST_ADDRESS`、`UTL_HTTP.REQUEST`、`HTTPURITYPE`以及`DBMS_LDAP.INIT`等函数,分别获取IP地址、发起HTTP请求、获取CLOB类型的数据以及初始化LDAP连接。 MySQL的...

    PLSQL程序设计.pdf (清晰版)

    **9.6 使用UTL_FILE软件包** UTL_FILE包用于处理文件输入输出操作。 以上是根据给定的文档内容总结出的PL/SQL程序设计的相关知识点,涵盖了从基础到进阶的各个方面。通过学习这些内容,可以帮助开发者更好地理解和...

    Oracle读取excel数据

    - 使用Java或者PL/SQL的外部过程,如Oracle的Java stored procedures或者Oracle的UTL_FILE包来读取Excel文件。 - 解析Excel文件内容,这可能需要第三方库,如Apache POI(Java)或者通过Oracle的外部表功能。 - ...

    Oracle通过LogMiner实现数据同步迁移

    创建一个名为`utlfile`的目录对象,并设置`utl_file_dir`参数,确保它可以访问之前创建的目录。 3. **开启日志追加模式**:执行`ALTER DATABASE ADD SUPPLEMENTAL LOG DATA;`命令,这使得数据库记录所有DDL和DML...

    Oracle SQL Recipes A Problem-Solution Approach

    - GROUP BY、HAVING和ORDER BY子句的使用方法。 3. **数据聚合与汇总** - 使用聚合函数(如SUM、AVG、COUNT等)对数据进行统计分析。 - 多表连接(JOIN)的方法及应用场景。 4. **高级查询技术** - 子查询...

Global site tag (gtag.js) - Google Analytics