0

I have an xml with the following type of content

<DATA>
  <CUSTOMER>
    <BASIC_INFO>
         <F_NAME>TOM<F_NAME>
         <M_NAME>AND<M_NAME>
         <L_NAME>HANKS<L_NAME>
    </BASIC_INFO>
    <ADDITIONAL_INFO>
         <EMAIL>TOM.HANKS@GMAIL.COM</EMAIL>
         <PHONE_NO>22211132</PHONE_NO>
    </ADDITIONAL_INFO>
  </CUSTOMER>

  <CUSTOMER>
    <BASIC_INFO>
         <F_NAME>TOM<F_NAME>
         <L_NAME>HANKS<L_NAME>
    </BASIC_INFO>
    <ADDITIONAL_INFO>
         <EMAIL>TOM.HANKS@GMAIL.COM</EMAIL>
    </ADDITIONAL_INFO>
  </CUSTOMER>

  <CUSTOMER>
    <BASIC_INFO>
         <F_NAME>TOM<F_NAME>
    </BASIC_INFO>
    <ADDITIONAL_INFO>
        <PHONE_NO>22211132</PHONE_NO>
    </ADDITIONAL_INFO>
  </CUSTOMER>

I want to store the information of a customer in the table with columns as F_NAME,M_NAME,L_NAME,PHONE_NO,EMAIL and each value should go in the respective column

Now the point to be noted is that for the customers for which some information is not there for them the value in the table should not be inserted.

4

1 回答 1

1

您可以分两步完成。

首先创建数据的关系表示:

来自sql-plsql-de.blogspot.co.at的示例:

create table xmltest (
  dokument xmltype
)
/

insert into xmltest values (xmltype(
'<blog>
  <name>SQL und PL/SQL</name>
  <autor>Carsten Czarski</autor>
  <themen>
    <thema>XML</thema>
    <thema>PL/SQL</thema>
  </themen>
</blog>'
))
/

这个说法:

select
  extractvalue(dokument, '/blog/name') as blog_name,
  extractvalue(dokument, '/blog/autor') as blog_autor,
  extractvalue(value(thema), '/thema/text()') as thema
from xmltest,
  table(xmlsequence(extract(dokument, '/blog/themen/thema'))) thema
/

重新荡妇:

BLOG_NAME            BLOG_AUTOR      THEMA
-------------------- --------------- --------------------
SQL und PL/SQL       Carsten Czarski XML
SQL und PL/SQL       Carsten Czarski PL/SQL

现在您以关系方式拥有它,您可以循环它:

for cur in (select * from ...) 
loop
  if cur.column_a is not null then
    --insert
  end if;
end loop;

这很慢,但即使你有复杂的规则也能保持可读性。或者,您可以使用“普通”SQL 或合并语句。

于 2013-03-12T12:04:18.970 回答