如何在存储过程中将xml字符串插入Oracle数据库?

时间:2018-07-19 06:49:37

标签: asp.net xml oracle oracle11g oracle10g

我的XML数据是:

<NewDataSet>
         <AotReversefeedback>
         <Refid>N161144</Refid>
         <DPID />
         <TrdAccOpenId>92021144            </TrdAccOpenId>
         <TrdAccOpenDate>May  7 2018 12:00AM </TrdAccOpenDate>
         <EntryDate>25/03/2018</EntryDate>
         <ITicketStatus>POA</ITicketStatus>
         <LastupdatedDate>07/05/2018</LastupdatedDate>
         <Status>ACTIVE</Status>
         </AotReversefeedback>
         <AotReversefeedback>
         <Refid>N202240</Refid>
         <DPID />
         <TrdAccOpenId>83082240            </TrdAccOpenId>
         <TrdAccOpenDate>May  7 2018 12:00AM </TrdAccOpenDate>
         <EntryDate>03/05/2018</EntryDate>
         <ITicketStatus>KRA</ITicketStatus>
         <LastupdatedDate>07/05/2018</LastupdatedDate>
         <Status>ACTIVE</Status>
         </AotReversefeedback>
</NewDataSet>

我的表结构是

 create table LMSDATA
 (
         refid      nvarchar2(20),
         DPID      NVARCHAR2(20),
         trdaccopenid  number(9),
         trdaccopendate    nvarchar2(20),
         entrydate        date,
         iticketstatus nvarchar2(20),
         lastupdateddate date,
         status nvarchar2(20)
 );

存储过程将获取包含xml数据的输入字符串。可以使用什么方法将XML数据插入表中?

1 个答案:

答案 0 :(得分:4)

会是这个吗?

WITH t AS 
    (SELECT XMLTYPE(
    '<NewDataSet>
        <AotReversefeedback>
            <Refid>N161144</Refid>
            <DPID />
            <TrdAccOpenId>92021144            </TrdAccOpenId>
            <TrdAccOpenDate>May  7 2018 12:00AM </TrdAccOpenDate>
            <EntryDate>25/03/2018</EntryDate>
            <ITicketStatus>POA</ITicketStatus>
            <LastupdatedDate>07/05/2018</LastupdatedDate>
            <Status>ACTIVE</Status>
        </AotReversefeedback>
        <AotReversefeedback>
            <Refid>N202240</Refid>
            <DPID />
            <TrdAccOpenId>83082240            </TrdAccOpenId>
            <TrdAccOpenDate>May  7 2018 12:00AM </TrdAccOpenDate>
            <EntryDate>03/05/2018</EntryDate>
            <ITicketStatus>KRA</ITicketStatus>
            <LastupdatedDate>07/05/2018</LastupdatedDate>
            <Status>ACTIVE</Status>
        </AotReversefeedback>
    </NewDataSet>') AS XML_DATA
FROM dual)
SELECT 
    refid,
    DPID,
    trdaccopenid,
    trdaccopendate,
    TO_DATE(entrydate_str, 'dd/mm/yyyy') AS entrydate,
    iticketstatus,
    TO_DATE(LastupdatedDate_str, 'dd/mm/yyyy') AS LastupdatedDate,
    status
FROM t
    CROSS JOIN XMLTABLE('/NewDataSet/AotReversefeedback' PASSING XML_DATA COLUMNS 
        refid      NVARCHAR2(20) PATH 'Refid',
        DPID      NVARCHAR2(20) PATH 'DPID',
        trdaccopenid  NUMBER(9) PATH 'TrdAccOpenId',
        trdaccopendate    NVARCHAR2(20) PATH 'TrdAccOpenDate',
        entrydate_str        VARCHAR2(15) PATH 'EntryDate',
        iticketstatus NVARCHAR2(20) PATH 'ITicketStatus',
        lastupdateddate_str VARCHAR2(15) PATH 'LastupdatedDate',
        status NVARCHAR2(20) PATH 'Status'      
 ) x;

结果:

REFID   DPID    TRDACCOPENID    TRDACCOPENDATE  ENTRYDATE   ITICKETSTATUS   LASTUPDATEDDATE STATUS
N161144     92021144    May  7 2018 12:00AM     25.03.2018  POA 07.05.2018  ACTIVE
N202240     83082240    May  7 2018 12:00AM     03.05.2018  KRA 07.05.2018  ACTIVE

ExtractValue也可以使用,但该功能已弃用。