1

我想使用 SQL 开发人员将 XML 文件导入 Oracle SQL。XML 有多个节点,我的 XML 结构:

<"<"SPECTRAEXCHANGE>">"
<"<"APPLICATION>">"
<"<"SV_SV_ID>">"kClong<"<"/SV_SV_ID/>">"
<"<"SS_SS_ID>">"kClong<"<"/SS_SS_ID/>">"
<"<"AP_NAME>">"kCstring (64)<"<"/AP_NAME/>">"
<"<"AP_PRJ_IDENT>">"kCstring (32)<"<"/AP_PRJ_IDENT/>">"
<"<"STATION>">"
<"<"TCS_NAME>">"kCstring (64)<"<"/TCS_NAME/>">"
<"<"TCS_CALL>">"kCstring (256)<"<"/TCS_CALL/>">"
<"<"HORIZONTAL_ELEVATIONS>">"
<"<"HORIZONTAL_ELEVATION>">"
<"<"HE_AZIMUT>">"kCdouble<"<"/HE_AZIMUT/>">"
<"<"HE_ELEVATION>">"kCdouble<"<"/HE_ELEVATION/>">"
<"<"/HORIZONTAL_ELEVATION/>">"
<"<"/HORIZONTAL_ELEVATIONS/>">"
<"<"TRANSMITTER>">"
<"<"EQP_EQUIP_NAME>">"kCstring (128)<"<"/EQP_EQUIP_NAME/>">"
<"<"EQP_EQUIP_TYPE>">"kCstring (16)<"<"/EQP_EQUIP_TYPE/>">"
<"<"FREQUENCY>">"
<"<"EFL_FREQ>">"kCdouble<"<"/EFL_FREQ/>">"
<"<"COORDINATED_FREQUENCY>">"
<"<"COF_DAT>">"kWrDate<"<"/COF_DAT/>">"
<"<"/COORDINATED_FREQUENCY/>">"
<"<"/FREQUENCY/>">"
<"<"/TRANSMITTER/>">"
<"<"/STATION/>">"
<"<"/APPLICATION/>">"
<"<"/SPECTRAEXCHANGE/>">" 

首先,我创建了表 SPECTRAEXCHANGE,然后我想使用以下命令:

INSERT INTO TABLENAME(SV_SV_ID,
SS_SS_ID,
AP_NAME,
AP_PRJ_IDENT )
WITH t AS (SELECT xmltype(bfilename('TEST_DIR','yourXMLFileName.xml'), nls_charset_id('WE8ISO8859P1')) xmlcol FROM dual)
SELECT
extractValue(value(x),'/APPLICATION/SV_SV_ID') SV_SV_ID /* value will be kClong */
,extractValue(value(x),'/APPLICATION/SS_SS_ID') SS_SS_ID /* value will be kClong*/
,extractValue(value(x),'APPLICATION/AP_NAME') AP_NAME /* value will be kCstring (64)*/
,extractValue(value(x),'APPLICATION/AP_PRJ_IDENT') AP_PRJ_IDENT
FROM t,TABLE(XMLSequence(extract(t.xmlcol,'/SPECTRAEXCHANGE/APPLICATION'))) x;


UPDATE SPECTRAEXCHANGE SET TCS_NAME = extractValue(value(x),'/APPLICATION/STATION/TCS_NAME'),
TCS_CALL = extractValue(value(x),'/APPLICATION/STATION/TCS_CALL')
WITH t AS (SELECT xmltype(bfilename('TEST_DIR','yourXMLFileName.xml'), nls_charset_id('WE8ISO8859P1')) xmlcol FROM dual)
FROM t,TABLE(XMLSequence(extract(t.xmlcol,'/SPECTRAEXCHANGE/APPLICATION/STATION'))) x;

ETC..

在 UPDATE 会话中,我收到错误消息:00933. 00000 - “SQL 命令未正确结束”

我在 UPDATE 会话中尝试了这个:

UPDATE SPECTRAEXCHANGE SET
TCS_NAME = x.TCS_NAME,
TCS_CALL = x.TCS_CALL
WITH t AS (SELECT xmltype(bfilename('TEST_DIR','yourXMLFileName.xml'), nls_charset_id('WE8ISO8859P1')) xmlcol FROM dual)
SELECT
extractValue(value(x),'APPLICATION/STATION/TCS_NAME') TCS_NAME
,extractValue(value(x),'APPLICATION/STATION/TCS_CALL') TCS_CALL
FROM t,TABLE(XMLSequence(extract(t.xmlcol,'/SPECTRAEXCHANGE/APPLICATION/STATION'))) x; 

但不幸的是它不起作用......所以你能帮我更新部分吗?或给我其他教程以导入具有多个节点的 xml。谢谢。米兰

4

1 回答 1

1

您正在尝试更新子查询中的两列,但您的语法错误;它应该更像:

update tablename set (col1 = val1, col2 = val2)
select (val1, val 2 from ...)

在您的情况下,假设您要插入和更新同一个表,并将(修改后的)原始 XML 作为 SQL*Plus var 传递给我的测试:

create table spectraexchange(sv_sv_id varchar2(15), ss_ss_id varchar2(15),
    ap_name varchar2(15), ap_prj_ident varchar2(15),
    tcs_name varchar2(15), tcs_call varchar2(15));

Table created.

insert into spectraexchange(sv_sv_id, ss_ss_id, ap_name, ap_prj_ident)
select extractvalue(value(x), 'APPLICATION/SV_SV_ID') sv_sv_id,
    extractvalue(value(x), 'APPLICATION/SS_SS_ID') ss_ss_id,
    extractvalue(value(x), 'APPLICATION/AP_NAME') ap_name,
    extractvalue(value(x), 'APPLICATION/AP_PRJ_IDENT') ap_prj_ident
from (
    select xmltype(:raw_xml) xmlcol from dual
) t
cross join table(XMLSequence(extract(t.xmlcol,
    '/SPECTRAEXCHANGE/APPLICATION'))) x;

1 row created.

select * from spectraexchange;

SV_SV_ID        SS_SS_ID        AP_NAME         AP_PRJ_IDENT    TCS_NAME        TCS_CALL
--------------- --------------- --------------- --------------- --------------- ---------------
kClong          kClong          kCstring (64)   kCstring (32)

那么更新可能是:

update spectraexchange
set (tcs_name, tcs_call) = (
    select extractvalue(value(x), 'STATION/TCS_NAME'),
        extractvalue(value(x), 'STATION/TCS_CALL')
    from (
        select xmltype(:raw_xml) xmlcol from dual
    ) t
    cross join table(XMLSequence(extract(t.xmlcol,
        '/SPECTRAEXCHANGE/APPLICATION/STATION'))) x
);

1 row updated.

select * from spectraexchange;

SV_SV_ID        SS_SS_ID        AP_NAME         AP_PRJ_IDENT    TCS_NAME        TCS_CALL
--------------- --------------- --------------- --------------- --------------- ---------------
kClong          kClong          kCstring (64)   kCstring (32)   kCstring (64)   kCstring (256)

如果当然这也假设每个应用程序有一个站点,否则您将需要多个连接表来保存我猜的关系;并且只需要以某种方式关联一个应用程序或您的更新。但是随后更新似乎毫无意义,您可以在插入时完成所有操作:

insert into spectraexchange(sv_sv_id, ss_ss_id, ap_name, ap_prj_ident,
    tcs_name, tcs_call)
select extractvalue(value(x), 'APPLICATION/SV_SV_ID') sv_sv_id,
    extractvalue(value(x), 'APPLICATION/SS_SS_ID') ss_ss_id,
    extractvalue(value(x), 'APPLICATION/AP_NAME') ap_name,
    extractvalue(value(x), 'APPLICATION/AP_PRJ_IDENT') ap_prj_ident,
    extractvalue(value(x), 'APPLICATION/STATION/TCS_NAME') tcs_name,
    extractvalue(value(x), 'APPLICATION/STATION/TCS_CALL') tcs_call
from (
    select xmltype(:raw_xml) xmlcol from dual
) t
cross join table(XMLSequence(extract(t.xmlcol,
    '/SPECTRAEXCHANGE/APPLICATION'))) x;

...(仅适用于一对一的关系)所以很明显我从图片中遗漏了一些东西。


根据您的评论,您有一对多关系并且您将所有内容插入到一个表中(!?),您可以这样做:

insert into spectra exchange ( ... columns ... )
select a.sv_sv_id, a.ss_ss_id, a.ap_name, a.ap_prj_ident,
    s.tcs_name, s.tcs_call,
    t.eqp_equip_name, t.eqp_equip_type
from (select xmltype(:raw_xml) xmlcol from dual) r
cross join xmltable('/SPECTRAEXCHANGE/APPLICATION' passing r.xmlcol
        columns sv_sv_id varchar2(15) path 'SV_SV_ID',
            ss_ss_id varchar2(15) path 'SS_SS_ID',
            ap_name varchar2(15) path 'AP_NAME',
            ap_prj_ident varchar2(15) path 'AP_PRJ_IDENT',
            stations xmltype path 'STATION'
    ) (+) a
cross join xmltable('/STATION' passing a.stations
        columns tcs_name varchar2(15) path 'TCS_NAME',
            tcs_call varchar2(15) path 'TCS_CALL',
            transmitter xmltype path 'TRANSMITTER'
    ) (+) s
cross join xmltable('/TRANSMITTER' passing s.transmitter
        columns eqp_equip_name varchar2(15) path 'EQP_EQUIP_NAME',
            eqp_equip_type varchar2(15) path 'EQP_EQUIP_TYPE',
            frequency xmltype path 'FREQUENCY'
    ) (+) t
/

我已经将发射器降低了一个额外的级别,您可以重复该模式以添加更多,每次都将相关节点向下传递。外部连接(+)将允许一些不存在的东西,例如,如果您有一个尚未被指定频率的发射器,或者其他什么 - 您将在相关列中得到一个空值。

于 2013-05-03T09:08:43.847 回答