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

2 つのステップでそれを行うことができます。

まず、データのリレーショナル表現を作成します。

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
/

Re sluts で:

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 に答える