Solved

XML to Table (MS SQL)

Posted on 2014-10-03
7
255 Views
Last Modified: 2015-08-04
Hi,
I have a xml file looking like this:
<?xml version="1.0" encoding="UTF-8" ?><database>
  <DELTACONO>
    <version>1.0</version>
    <data information="Items">
      <items>
        <item id="03-TP">
          <description>
            <short><![CDATA[DELTACO U/UTP Cat5e patchkabel 0,3m, grå]]></short>
          </description>
          <ean>7340004657330</ean>
        </item>
        <item id="05-6420">
          <description>
            <short><![CDATA[Direktronik Swing - Koblingskabel - RJ-45 (hann) - RJ-45 (hann) - 50 cm - UTP - CAT 5e - grå]]></short>
          </description>
        </item>

With closings of course.

I use this query:

DECLARE @xmlData AS XML

SET @xmlData = (
  SELECT * FROM OPENROWSET (
    BULK 'D:\engine\importfiler\levInfo\deltaco\item2.xml', SINGLE_CLOB
  ) AS xmlData
)

SELECT ref.value('./item[1]', 'varchar(50)') AS SKU,
         ref.value('./ean[1]', 'varchar(50)') AS Ean
FROM @xmlData.nodes('/database/DELTACONO/data') AS Data(ref)

But i'm unable to get the data I want.
I only get 1 empty row.

This is how I want it to look:

SKU         EAN
03-TP      7340004657330
05-6420  

I'm pretty new to XML so a little help is needed...
0
Comment
Question by:Petter A. Halseth
  • 4
  • 3
7 Comments
 
LVL 14

Accepted Solution

by:
Christopher Gordon earned 500 total points
ID: 40359297
Try this:

SELECT
      ref.value('@id[1]', 'varchar(50)') AS SKU,
        ref.value('./ean[1]', 'varchar(50)') AS Ean

FROM @xmlData.nodes('/database/DELTACONO/data/items/item') AS Data(ref)

Note the difference between pulling values from XML attribute and XML element value.
0
 

Author Comment

by:Petter A. Halseth
ID: 40359330
Ah.. nice. I got it to work thanx!
I see that I also need to pull the <short> node as well. How can i do this so I get this on the same line?

Like this:
SKU         EAN                     Short
03-TP      7340004657330   DELTACO U/UTP Cat5e patchkabel 0,3m, grå
05-6420                               Direktronik Swing - Koblingskabel - RJ-45 (hann) - RJ-45 (hann) - 50 cm - UTP - CAT 5e - grå
P.
0
 
LVL 14

Expert Comment

by:Christopher Gordon
ID: 40359352
SELECT
            ref.value('@id[1]', 'varchar(50)') AS SKU,
        ref.value('./ean[1]', 'varchar(50)') AS Ean,
            ref.value('.', 'nvarchar(50)') as short

FROM @xmlData.nodes('/database/DELTACONO/data/items/item') AS Data(ref)
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:Petter A. Halseth
ID: 40359375
This xml thing is really confusing... but I'm getting there step by step.

I have a similar xml file with attributes and element values that look like this:
<DELTACONO>
    <version>1.0</version>
    <data information="Items Stock Price">
      <items>
        <item id="03-TP">
          <stock quantity="3874" />
          <price>
            <net>4,47</net>
          </price>
        </item>

I'm trying to get the output to look like this:

SKU    StockQuantity   NetPrice
03-TP  3874                  4,47

using this query:
SELECT  ref.value('@id[1]', 'varchar(50)') AS SKU,
            ref.value('@quantity[1]', 'varchar(50)') AS Stock,
            ref.value('.', 'varchar(50)') AS Price
FROM @xmlData.nodes('/database/DELTACONO/data/items/item') AS Data(ref)

But then I'm only get the SKU and Price. Stock comes up as NULL

P.
0
 
LVL 14

Expert Comment

by:Christopher Gordon
ID: 40360159
Sorry for delay,  got caught up in stuff...

If you still need help on this, here is some more code that might help:

SELECT  
            ref.value('@id[1]', 'varchar(50)') AS SKU,
            
            ref.value('(stock/@quantity)[1]', 'int') as stock,

            ref.value('(price/net)[1]', 'varchar(100)') as price,

            ref.value('(description/short)[1]', 'varchar(1000)') as descr
                         
FROM @xmlData.nodes('/database/DELTACONO/data/items/item') AS Data(ref)
0
 

Author Comment

by:Petter A. Halseth
ID: 40363345
Hi,
Great thanx! It solved my problems!

P.
0
 

Author Comment

by:Petter A. Halseth
ID: 40914857
Hi, another problem popped up here. Received another xml that I need to import as well.
This time it looks like this:

<Body>
        <Line ID="10">
            <ItemID Type="Techdata">2869909</ItemID>
            <ItemID Type="Manufacturer">F7N060B2C00</ItemID>
            <ItemID Type="EAN">722868996157</ItemID>
            <ItemID Type="CommodityCode">4202918090</ItemID>
            <CountryOrigin>TW</CountryOrigin>
            <ItemText>Cinema Stripe Cover Blacktop iPad Air</ItemText>
            <ItemQty Type="delivered">5</ItemQty>
            <ItemWeight Type="Net" Unit="kg">1.6</ItemWeight>
            <ItemReference>
                <CustLine>1</CustLine>
            </ItemReference>
        </Line>

And I need to extract these data:
            <ItemID Type="Techdata">2869909</ItemID>
            <ItemID Type="Manufacturer">F7N060B2C00</ItemID>
            <ItemID Type="EAN">722868996157</ItemID>
            <ItemID Type="CommodityCode">4202918090</ItemID>
How can I get the data in these lines?

Any help is very much appreciated.

Petter.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now