Oracle UTF8 文件编码 Utl_file

Oracle UTF8 File Encoding With Utl_file

我想将我的文件导出为 UTF8 编码。 当我查看 v$nls_parameters 时,nls_characterset 是 WE8ISO8859P9。所以我的文件编码是 Ansii.

如何在不更改 v$nls_parameters 的情况下使用 UTF8 编码导出文件?

DECLARE
 v_os_touch_file                  utl_file.file_type;
 p_in_file                        VARCHAR2(50);
BEGIN
    v_os_touch_file := NULL;
    p_in_file := NULL;

    p_in_file := 'my_file_' || sysdate;
    v_os_touch_file := utl_file.fopen(my_path, p_in_file, 'w');
    FOR i IN (
        SELECT
            *
        FROM
            my_table
    ) LOOP
    begin

        utl_file.put_line(v_os_touch_file, 'TEST' );
        utl_file.put_line(v_os_touch_file, i.input);
         utl_file.fclose(v_os_touch_file);
END

使用UTL_FILE.FOPEN_NCHAR

Even though the contents of an NVARCHAR2 buffer may be AL16UTF16 or UTF8 (depending on the national character set of the database), the contents of the file are always read and written in UTF8. UTL_FILE converts between UTF8 and AL16UTF16 as necessary.

注意,使用 p_in_file := 'my_file_' || sysdate; 不是很聪明,因为它依赖于当前用户会话 NLS_DATE_FORMAT 设置。假设默认格式是 MM/DD/YYYY.

最好像 p_in_file := 'my_file_' || TO_CHAR(sysdate, 'YYYYMMDD'); 这样使用。