如何在oracle中使用utl_file写入文件

发布于 2024-09-19 19:02:26 字数 58 浏览 3 评论 0原文

如何使用 put 函数。我的程序没有用 put 编译。但 putline 工作正常。我想在同一行中打印

How to use put function.my procedure is not compiling with put. but putline is working fine. i want to print in the same line

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

阳光的暖冬 2024-09-26 19:02:26

下面是使用 UTL_FILE.PUT 和 UTL_FILE.PUT_LINE 调用的代码示例:

declare 
  fHandle  UTL_FILE.FILE_TYPE;
begin
  fHandle := UTL_FILE.FOPEN('my_directory', 'test_file', 'w');

  UTL_FILE.PUT(fHandle, 'This is the first line');
  UTL_FILE.PUT(fHandle, 'This is the second line');
  UTL_FILE.PUT_LINE(fHandle, 'This is the third line');

  UTL_FILE.FCLOSE(fHandle);
EXCEPTION
  WHEN OTHERS THEN
    DBMS_OUTPUT.PUT_LINE('Exception: SQLCODE=' || SQLCODE || '  SQLERRM=' || SQLERRM);
    RAISE;
end;

输出如下所示:

This is the first lineThis is the second lineThis is the third line

分享并享受。

Here's an example of code which uses the UTL_FILE.PUT and UTL_FILE.PUT_LINE calls:

declare 
  fHandle  UTL_FILE.FILE_TYPE;
begin
  fHandle := UTL_FILE.FOPEN('my_directory', 'test_file', 'w');

  UTL_FILE.PUT(fHandle, 'This is the first line');
  UTL_FILE.PUT(fHandle, 'This is the second line');
  UTL_FILE.PUT_LINE(fHandle, 'This is the third line');

  UTL_FILE.FCLOSE(fHandle);
EXCEPTION
  WHEN OTHERS THEN
    DBMS_OUTPUT.PUT_LINE('Exception: SQLCODE=' || SQLCODE || '  SQLERRM=' || SQLERRM);
    RAISE;
end;

The output from this looks like:

This is the first lineThis is the second lineThis is the third line

Share and enjoy.

ペ泪落弦音 2024-09-26 19:02:26

这是一个使用 UTL_File.putline 的强大函数,其中包括必要的错误处理。它还处理页眉、页脚和其他一些特殊情况。

PROCEDURE usp_OUTPUT_ToFileAscii(p_Path IN VARCHAR2, p_FileName IN VARCHAR2, p_Input IN refCursor, p_Header in VARCHAR2, p_Footer IN VARCHAR2, p_WriteMode VARCHAR2) IS

              vLine VARCHAR2(30000);
              vFile UTL_FILE.file_type; 
              vExists boolean;
              vLength number;
              vBlockSize number;
    BEGIN

        UTL_FILE.fgetattr(p_path, p_FileName, vExists, vLength, vBlockSize);

                 FETCH p_Input INTO vLine;
         IF p_input%ROWCOUNT > 0
         THEN
            IF vExists THEN
               vFile := UTL_FILE.FOPEN_NCHAR(p_Path, p_FileName, p_WriteMode);
            ELSE
               --even if the append flag is passed if the file doesn't exist open it with W.
                vFile := UTL_FILE.FOPEN(p_Path, p_FileName, 'W');
            END IF;
            --GET HANDLE TO FILE
            IF p_Header IS NOT NULL THEN 
              UTL_FILE.PUT_LINE(vFile, p_Header);
            END IF;
            UTL_FILE.PUT_LINE(vFile, vLine);
            DBMS_OUTPUT.PUT_LINE('Record count > 0');

             --LOOP THROUGH CURSOR VAR
             LOOP
                FETCH p_Input INTO vLine;

                EXIT WHEN p_Input%NOTFOUND;

                UTL_FILE.PUT_LINE(vFile, vLine);

             END LOOP;


             IF p_Footer IS NOT NULL THEN 
                UTL_FILE.PUT_LINE(vFile, p_Footer);
             END IF;

             CLOSE p_Input;
             UTL_FILE.FCLOSE(vFile);
        ELSE
          DBMS_OUTPUT.PUT_LINE('Record count = 0');

        END IF; 


    EXCEPTION
       WHEN UTL_FILE.INVALID_PATH THEN 
           DBMS_OUTPUT.PUT_LINE ('invalid_path'); 
           DBMS_OUTPUT.PUT_LINE(SQLERRM);
           RAISE;

       WHEN UTL_FILE.INVALID_MODE THEN 
           DBMS_OUTPUT.PUT_LINE ('invalid_mode'); 
           DBMS_OUTPUT.PUT_LINE(SQLERRM);
           RAISE;

       WHEN UTL_FILE.INVALID_FILEHANDLE THEN 
           DBMS_OUTPUT.PUT_LINE ('invalid_filehandle'); 
           DBMS_OUTPUT.PUT_LINE(SQLERRM);
           RAISE;

       WHEN UTL_FILE.INVALID_OPERATION THEN 
           DBMS_OUTPUT.PUT_LINE ('invalid_operation'); 
           DBMS_OUTPUT.PUT_LINE(SQLERRM);
           RAISE;

       WHEN UTL_FILE.READ_ERROR THEN  
           DBMS_OUTPUT.PUT_LINE ('read_error');
           DBMS_OUTPUT.PUT_LINE(SQLERRM);
           RAISE;

       WHEN UTL_FILE.WRITE_ERROR THEN 
          DBMS_OUTPUT.PUT_LINE ('write_error'); 
          DBMS_OUTPUT.PUT_LINE(SQLERRM);
           RAISE;

       WHEN UTL_FILE.INTERNAL_ERROR THEN 
          DBMS_OUTPUT.PUT_LINE ('internal_error'); 
          DBMS_OUTPUT.PUT_LINE(SQLERRM);
          RAISE;            
       WHEN OTHERS THEN
          DBMS_OUTPUT.PUT_LINE ('other write error'); 
          DBMS_OUTPUT.PUT_LINE(SQLERRM);
          RAISE;
    END;

Here is a robust function for using UTL_File.putline that includes the necessary error handling. It also handles headers, footers and a few other exceptional cases.

PROCEDURE usp_OUTPUT_ToFileAscii(p_Path IN VARCHAR2, p_FileName IN VARCHAR2, p_Input IN refCursor, p_Header in VARCHAR2, p_Footer IN VARCHAR2, p_WriteMode VARCHAR2) IS

              vLine VARCHAR2(30000);
              vFile UTL_FILE.file_type; 
              vExists boolean;
              vLength number;
              vBlockSize number;
    BEGIN

        UTL_FILE.fgetattr(p_path, p_FileName, vExists, vLength, vBlockSize);

                 FETCH p_Input INTO vLine;
         IF p_input%ROWCOUNT > 0
         THEN
            IF vExists THEN
               vFile := UTL_FILE.FOPEN_NCHAR(p_Path, p_FileName, p_WriteMode);
            ELSE
               --even if the append flag is passed if the file doesn't exist open it with W.
                vFile := UTL_FILE.FOPEN(p_Path, p_FileName, 'W');
            END IF;
            --GET HANDLE TO FILE
            IF p_Header IS NOT NULL THEN 
              UTL_FILE.PUT_LINE(vFile, p_Header);
            END IF;
            UTL_FILE.PUT_LINE(vFile, vLine);
            DBMS_OUTPUT.PUT_LINE('Record count > 0');

             --LOOP THROUGH CURSOR VAR
             LOOP
                FETCH p_Input INTO vLine;

                EXIT WHEN p_Input%NOTFOUND;

                UTL_FILE.PUT_LINE(vFile, vLine);

             END LOOP;


             IF p_Footer IS NOT NULL THEN 
                UTL_FILE.PUT_LINE(vFile, p_Footer);
             END IF;

             CLOSE p_Input;
             UTL_FILE.FCLOSE(vFile);
        ELSE
          DBMS_OUTPUT.PUT_LINE('Record count = 0');

        END IF; 


    EXCEPTION
       WHEN UTL_FILE.INVALID_PATH THEN 
           DBMS_OUTPUT.PUT_LINE ('invalid_path'); 
           DBMS_OUTPUT.PUT_LINE(SQLERRM);
           RAISE;

       WHEN UTL_FILE.INVALID_MODE THEN 
           DBMS_OUTPUT.PUT_LINE ('invalid_mode'); 
           DBMS_OUTPUT.PUT_LINE(SQLERRM);
           RAISE;

       WHEN UTL_FILE.INVALID_FILEHANDLE THEN 
           DBMS_OUTPUT.PUT_LINE ('invalid_filehandle'); 
           DBMS_OUTPUT.PUT_LINE(SQLERRM);
           RAISE;

       WHEN UTL_FILE.INVALID_OPERATION THEN 
           DBMS_OUTPUT.PUT_LINE ('invalid_operation'); 
           DBMS_OUTPUT.PUT_LINE(SQLERRM);
           RAISE;

       WHEN UTL_FILE.READ_ERROR THEN  
           DBMS_OUTPUT.PUT_LINE ('read_error');
           DBMS_OUTPUT.PUT_LINE(SQLERRM);
           RAISE;

       WHEN UTL_FILE.WRITE_ERROR THEN 
          DBMS_OUTPUT.PUT_LINE ('write_error'); 
          DBMS_OUTPUT.PUT_LINE(SQLERRM);
           RAISE;

       WHEN UTL_FILE.INTERNAL_ERROR THEN 
          DBMS_OUTPUT.PUT_LINE ('internal_error'); 
          DBMS_OUTPUT.PUT_LINE(SQLERRM);
          RAISE;            
       WHEN OTHERS THEN
          DBMS_OUTPUT.PUT_LINE ('other write error'); 
          DBMS_OUTPUT.PUT_LINE(SQLERRM);
          RAISE;
    END;
冷月断魂刀 2024-09-26 19:02:26

您可以查看以下示例,了解如何在 PL/SQL 中使用 utl_file 进行记录/写入。

DECLARE
fhandle  utl_file.file_type;
BEGIN
fhandle := utl_file.fopen(
                   'UTL_DIR_STACKOVERFLOW'-- File location
                 , 'Check_Logging.txt'    -- File name
                 , 'a'                    -- Open mode: a = append, w = write
                     );
utl_file.put(fhandle, 'Stackoverflow' );
utl_file.fclose(fhandle);
END;

您可能面临的问题 -


无效的目录对象

 Then you can create your own directory object and use the below query.

 create or replace directory UTL_DIR_STACKOVERFLOW as '/tmp/';

在此处输入图像描述

You can check the below example of how to log/write using utl_file in PL/SQL.

DECLARE
fhandle  utl_file.file_type;
BEGIN
fhandle := utl_file.fopen(
                   'UTL_DIR_STACKOVERFLOW'-- File location
                 , 'Check_Logging.txt'    -- File name
                 , 'a'                    -- Open mode: a = append, w = write
                     );
utl_file.put(fhandle, 'Stackoverflow' );
utl_file.fclose(fhandle);
END;

Problems you can face-


Invalid directory object

 Then you can create your own directory object and use the below query.

 create or replace directory UTL_DIR_STACKOVERFLOW as '/tmp/';

enter image description here

陪你到最终 2024-09-26 19:02:26
CREATE OR REPLACE PROCEDURE SP_EXPORT_TO_CSV(P_OWNER       VARCHAR2,
                                                            P_OBJECT_NAME VARCHAR2,
                                                            P_FILE_NAME   VARCHAR2 --,
                                                            --   P_DELIMITED CHAR
                                                            ) AS

  -- declaration
  TYPE C_DATA_CURSOR IS REF CURSOR;
  C_DATA C_DATA_CURSOR;

  v_file        UTL_FILE.FILE_TYPE;
  V_COLUMNS     VARCHAR2(32767);
  X             VARCHAR2(32767);
  V_FILE_NAME   VARCHAR2(2000);
  V_OWNER       VARCHAR2(100);
  V_OBJECT_NAME VARCHAR2(1000);
  V_SQL         VARCHAR2(32767);
  V_DELIMITED   VARCHAR2(10);
BEGIN
  -- set value
  V_FILE_NAME   := P_FILE_NAME;
  V_OBJECT_NAME := P_OBJECT_NAME;
  V_OWNER       := P_OWNER;
  --V_DELIMITED:=P_DELIMITED;

  SELECT REPLACE(LISTAGG(COLUMN_NAME, '|') WITHIN
                 GROUP(ORDER BY COLUMN_ID),
                 '|',
                 ' || ' || '''|''' || ' || ')
    INTO V_COLUMNS
    FROM ALL_TAB_COLUMNS W
   WHERE W.TABLE_NAME = V_OBJECT_NAME
     AND W.OWNER = V_OWNER
   ORDER BY COLUMN_ID;

  -- SET OUTPUT PARAMETER --'EXPORTED_TO_TEXT_LOCATION'
  v_file := UTL_FILE.FOPEN(location     => 'MYLOCATION',
                           filename     => V_FILE_NAME,
                           open_mode    => 'w',
                           max_linesize => 32767);
  UTL_FILE.PUT_LINE(v_file, REPLACE(V_COLUMNS, ' || ''|'' || ', '|'));
  X := 'SELECT ' || V_COLUMNS || ' AS RECORD FROM ' || V_OWNER || '.' ||
       V_OBJECT_NAME;

  OPEN C_DATA FOR X;
  LOOP
    FETCH C_DATA
      INTO V_SQL;
    EXIT WHEN C_DATA%NOTFOUND;
    UTL_FILE.PUT_LINE(v_file, V_SQL);
  END LOOP;
  CLOSE C_DATA;

  UTL_FILE.FCLOSE(v_file);

 
END;
CREATE OR REPLACE PROCEDURE SP_EXPORT_TO_CSV(P_OWNER       VARCHAR2,
                                                            P_OBJECT_NAME VARCHAR2,
                                                            P_FILE_NAME   VARCHAR2 --,
                                                            --   P_DELIMITED CHAR
                                                            ) AS

  -- declaration
  TYPE C_DATA_CURSOR IS REF CURSOR;
  C_DATA C_DATA_CURSOR;

  v_file        UTL_FILE.FILE_TYPE;
  V_COLUMNS     VARCHAR2(32767);
  X             VARCHAR2(32767);
  V_FILE_NAME   VARCHAR2(2000);
  V_OWNER       VARCHAR2(100);
  V_OBJECT_NAME VARCHAR2(1000);
  V_SQL         VARCHAR2(32767);
  V_DELIMITED   VARCHAR2(10);
BEGIN
  -- set value
  V_FILE_NAME   := P_FILE_NAME;
  V_OBJECT_NAME := P_OBJECT_NAME;
  V_OWNER       := P_OWNER;
  --V_DELIMITED:=P_DELIMITED;

  SELECT REPLACE(LISTAGG(COLUMN_NAME, '|') WITHIN
                 GROUP(ORDER BY COLUMN_ID),
                 '|',
                 ' || ' || '''|''' || ' || ')
    INTO V_COLUMNS
    FROM ALL_TAB_COLUMNS W
   WHERE W.TABLE_NAME = V_OBJECT_NAME
     AND W.OWNER = V_OWNER
   ORDER BY COLUMN_ID;

  -- SET OUTPUT PARAMETER --'EXPORTED_TO_TEXT_LOCATION'
  v_file := UTL_FILE.FOPEN(location     => 'MYLOCATION',
                           filename     => V_FILE_NAME,
                           open_mode    => 'w',
                           max_linesize => 32767);
  UTL_FILE.PUT_LINE(v_file, REPLACE(V_COLUMNS, ' || ''|'' || ', '|'));
  X := 'SELECT ' || V_COLUMNS || ' AS RECORD FROM ' || V_OWNER || '.' ||
       V_OBJECT_NAME;

  OPEN C_DATA FOR X;
  LOOP
    FETCH C_DATA
      INTO V_SQL;
    EXIT WHEN C_DATA%NOTFOUND;
    UTL_FILE.PUT_LINE(v_file, V_SQL);
  END LOOP;
  CLOSE C_DATA;

  UTL_FILE.FCLOSE(v_file);

 
END;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文