Can Oracle SQL*Loader process XML?

妖精的绣舞 提交于 2019-12-08 04:39:25

No, SQL*Loader can only process "flat" files.

One option is to write an XSLT transformation that turns the ROWSET/ROW/column format into a text file and then import that into the target table.

Another option is to import the XML into a single row, and then use Oracle's XML functions to select a relational result from that staging table and insert it into the real table.

There isn't a standardised option, but with this specific format you can do it. If you have a table:

CREATE TABLE test_tab (
    id NUMBER,
    text VARCHAR2(50)
);

And your records in a test_tab.xml file:

<ROWSET>
<ROW>
<ID>1</ID>
<TEXT>This is some text</TEXT>
</ROW>
<ROW>
<ID>2</ID>
<TEXT>This is some more text</TEXT>
</ROW>
<ROW>
<ID>3</ID>
<TEXT>This is some other text</TEXT>
</ROW>
<ROW>
<ID>4</ID>
<TEXT>This is also some text</TEXT>
</ROW>
</ROWSET>

And a control file test_tab.ctl:

LOAD DATA
INFILE 'test_tab.xml'
CONCATENATE 4
INTO TABLE test_tab
(
    dummy FILLER CHAR(15) TERMINATED BY "<ROW>",
    id CHAR(10) ENCLOSED BY "<ID>" AND "</ID>",
    text CHAR(40) ENCLOSED BY "<TEXT>" AND "</TEXT>"
)

You can do:

sqlldr usr/pwd control=test_tab.ctl

Commit point reached - logical record count 4

SELECT * FROM test_tab;

        ID TEXT
---------- --------------------------------------------------
         1 This is some text
         2 This is some more text
         3 This is some other text
         4 This is also some text

You could also create an external table from the same file, if you put in a directory Oracle can see:

CREATE TABLE test_tab (
    id NUMBER,
    text VARCHAR2(50)
)
ORGANIZATION EXTERNAL
(
    TYPE ORACLE_LOADER
    DEFAULT DIRECTORY some_dir
    ACCESS PARAMETERS
    (
        RECORDS DELIMITED BY "</ROW>"
        FIELDS
        (
            dummy CHAR(15) TERMINATED BY "<ROW>",
            id CHAR(10) ENCLOSED BY "<ID>" AND "</ID>",
            text CHAR(40) ENCLOSED BY "<TEXT>" AND "</TEXT>"
        )
    )
    LOCATION ('test_tab.xml')
)
PARALLEL
REJECT LIMIT UNLIMITED;

Table created.

SELECT * FROM test_tab;

    ID TEXT
---------- --------------------------------------------------
     1 This is some text
     2 This is some more text
     3 This is some other text
     4 This is also some text
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!