4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Oracle PL/SQL ファイル操作の例

Posted at

#PL/SQL ファイル操作
PL/SQLにて、UTL_FILEパッケージを用いることによりファイルの入出力を行うことができます。
例としてログを残すなどの使い方ができます。

※1. 実行前に初期化パラメータUTL_FILE_DIRを設定する必要があります。
※2. UTL_FILE_DIRパラメータの設定後は、データベースの再起動が必要になります。

##ファイル書き込み

Write_File.sql
CREATE PROCEDURE Write_File
IS
    FILE_HD_W UTL_FILE.FILE_TYPE;
BEGIN

    FILE_HD_W := UTL_FILE.FOPEN('D:¥PLSQL','Write_File.txt','w');

    UTL_FILE.PUT(FILE_HD_W,'書き込みました');
    UTL_FILE.FCLOSE(FILE_HD_W);

END;
/

##ファイルに追記

Write_File_Add.sql
CREATE PROCEDURE Write_File_Add
IS
    FILE_HD_A UTL_FILE.FILE_TYPE;
BEGIN

    FILE_HD_A := UTL_FILE.FOPEN('D:¥PLSQL','Write_File.txt','a');

    UTL_FILE.NEW_LINE(FILE_HD_A,3);
    UTL_FILE.PUT(FILE_HD_A,'追記しました');
    UTL_FILE.FCLOSE(FILE_HD_A);

END;
/

##ファイル読み込み

Read_File.sql
CREATE PROCEDURE Read_File
IS
    FILE_HD_R UTL_FILE.FILE_TYPE;
    READ_DATA VARCHAR2(250);
BEGIN

    FILE_HD_R := UTL_FILE.FOPEN('D:¥PLSQL','Read_File.txt','r');

    UTL_FILE.GET_LINE(FILE_HD_R,READ_DATA);
    DBMS_OUTPUT.PUT_LINE(READ_DATA);
    UTL_FILE.FCLOSE(FILE_HD_R);

END;
/
4
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?