oracle 获得ORA-29280和UTL_FILE

tkqqtvp1  于 2023-10-16  发布在  Oracle
关注(0)|答案(2)|浏览(144)

我在运行以下代码时遇到问题:我正在尝试做一个关于UTL_FILE用法的理论练习,我打算将XML写入文件。

DECLARE
        XML_VAL01 XMLTYPE;
        XML_VALUE CLOB;
        VAR_DIRID VARCHAR(10) := '&FOLDER_NM';
        VAR_DIRID_CHK NUMBER;
        VAR_FILEPATH VARCHAR(200);
        VAR_FILENAME VARCHAR(30) := '&SPL_NM';
        VAR_FILEEXTS VARCHAR(4) := 'XML';

        L_FILE UTL_FILE.FILE_TYPE;
    BEGIN

    -- This is my XML File --
        SELECT 
            XMLSERIALIZE(CONTENT
            XMLELEMENT("TestParameters",
                XMLELEMENT("Parameter2", V1),
                XMLELEMENT("Parameter3", XMLATTRIBUTES(V3"Attr1", V4"Attr4"), V2),
                XMLELEMENT("Forest1", XMLFOREST('For1' AS V5, 'For2' AS V6, XMLCDATA(V7) AS "For3")),
                XMLAGG(XMLELEMENT("NextNumber1", V99) ORDER BY V99) --*/
            ) AS CLOB INDENT)
            INTO XML_VALUE
        FROM (SELECT 'Value1' V1, 'Value2' V2, 'Value3' V3, 'Value4' V4, 'Value5' V5, 'Value6' V6, 'Value7' V7, LEVEL V99 FROM DUAL CONNECT BY LEVEL <= 5)
        GROUP BY V1, V2, V3, V4, V5, V6, V7;

    -- Here I am validating if the Directory Exists
        SELECT COUNT(*) INTO VAR_DIRID_CHK FROM ALL_DIRECTORIES WHERE DIRECTORY_NAME = VAR_DIRID;

        IF VAR_DIRID_CHK <= 0 THEN
            DBMS_OUTPUT.PUT_LINE('No Directory');
            DBMS_OUTPUT.PUT_LINE(XML_VALUE);

        ELSE 
            -- If my directory exists, I will write the XML
            SELECT DIRECTORY_PATH INTO VAR_FILEPATH FROM ALL_DIRECTORIES WHERE DIRECTORY_NAME = VAR_DIRID AND OWNER = 'SYS';

            L_FILE := UTL_FILE.FOPEN (VAR_FILEPATH, VAR_FILENAME || '.' || VAR_FILEEXTS, 'W');
                UTL_FILE.PUT_LINE(L_FILE, XML_VALUE);
                UTL_FILE.FCLOSE(L_FILE);
        END IF;
    END;
    /

为了确保我的目录是有效的,我使用ALL_DIRECTORIES。不过,我还是得到了:

Error report -
ORA-29280: invalid directory object
ORA-06512: at "SYS.UTL_FILE", line 41
ORA-06512: at "SYS.UTL_FILE", line 478
ORA-06512: at line 36
29280. 00000 -  "invalid directory object"
*Cause:    A corresponding directory object does not exist.
*Action:   Correct the directory object parameter, or create a corresponding
           directory object with the CREATE DIRECTORY command.

我已经验证过我有权访问文件夹。

这里的目录:

谢谢

yeotifhr

yeotifhr1#

Oracle文档说明了utl_file.fopen的第一个参数:
文件的目录位置。此字符串是目录对象名称,必须以大写形式指定。必须授予UTL_FILE用户对该目录对象的读取权限才能运行FOPEN。
它要求输入目录名(all_directories.directory_name),而不是文件路径(all_directories.directory_path)。提供VAR_DIRID作为参数,它应该工作,假设网络路径可访问Oracle运行在这台机器上,Oracle接受该路径语法。如果没有,请尝试将驱动器Map到它,并将驱动器号分配给Oracle目录。无论哪种方式,通过传入目录名,Oracle将通过查看指定的目录对象将名称转换为真实的路径。还要确保你已经向代码所有者授予了读和写权限:

grant read on directory pimtest to SCHEMANAME;
grant write on directory pimtest to SCHEMANAME;
pdsfdshx

pdsfdshx2#

不要使用远程磁盘路径,只需使用Map网络驱动器来模拟本地驱动器,方法是为它分配一个驱动器号并在Oracle中Map它。

相关问题