sql-serversql-server-2016sql-server-2016-localdb

Import XML to SQL Server Table with attributes


I need help with import data from XML to SQL Server table. I only manage to import one row for the first match and need help to understand how to proceed to get all "rows" imported..

The data I would like to insert look like this and is saved in a file on my disc but I will later want to get the file from an url:

<result>
<data>
    <s id="16" n="Ice Hockey">
        <cat id="1060" n="Swedish SHL">
            <m id="1335939" n="Farjestad BK v Hv71" mid="804927" dt="2017-03-04T15:00">
                <t id="1" n="Single Match">
                    <l id="23578344" i="0" o="3.9">Draw</l>
                    <l id="23578343" i="1" o="2.35">Farjestad BK</l>
                    <l id="23578342" i="2" o="2.45">Hv71</l>
                </t>
                <t id="2" n="Moving Line">
                    <l id="23578350" i="1" o="3.5" p="-1.5">Farjestad BK (-1.5)</l>
                    <l id="23578349" i="2" o="1.25" p="1.5">Hv71 (+1.5)</l>
                </t>
                <t id="4" n="Point Score">
                    <l id="23578348" i="1" o="1.8" p="4.5">Game Total OVER 4.5</l>
                    <l id="23578347" i="2" o="2" p="4.5">Game Total UNDER 4.5</l>
                </t>
            </m>
        </cat>
        <cat id="1062" n="Finnish SM-liiga">
            <m id="1335991" n="Jukurit v Kookoo" mid="804993" dt="2017-03-04T15:00">
                <t id="1" n="Single Match">
                    <l id="23579183" i="0" o="4">Draw</l>
                    <l id="23579182" i="1" o="1.8">Jukurit</l>
                    <l id="23579181" i="2" o="3.5">Kookoo</l>
                </t>
                <t id="2" n="Moving Line">
                    <l id="23579189" i="1" o="2.55" p="-1.5">Jukurit (-1.5)</l>
                    <l id="23579188" i="2" o="1.45" p="1.5">Kookoo (+1.5)</l>
                </t>
                <t id="4" n="Point Score">
                    <l id="23579187" i="1" o="1.9" p="4.5">Game Total OVER 4.5</l>
                    <l id="23579186" i="2" o="1.8" p="4.5">Game Total UNDER 4.5</l>
                </t>
            </m>
            <m id="1335996" n="Ässät Pori v HPK Hämeenlinna" mid="804998" dt="2017-03-04T15:00">
                <t id="1" n="Single Match">
                    <l id="23579278" i="0" o="3.9">Draw</l>
                    <l id="23579277" i="1" o="2.25">Ässät Pori</l>
                    <l id="23579276" i="2" o="2.65">HPK Hämeenlinna</l>
                </t>
                <t id="2" n="Moving Line">
                    <l id="23579284" i="1" o="3.2" p="-1.5">Ässät Pori (-1.5)</l>
                    <l id="23579283" i="2" o="1.3" p="1.5">HPK Hämeenlinna (+1.5)</l>
                </t>
                <t id="4" n="Point Score">
                    <l id="23579282" i="1" o="1.9" p="4.5">Game Total OVER 4.5</l>
                    <l id="23579281" i="2" o="1.8" p="4.5">Game Total UNDER 4.5</l>
                </t>
            </m>
        </cat>
    </s>
</data>

I managed to import the "first row" but not the other two lines from the code above. My code looks like this:

    INSERT INTO dbo.Intertops(SportID, SportName,CatID, CatName, MatchID, MatchName,
    MatchMID, MatchDate, TypeID, TypeName, LineID, LineIndex, LineOdds, Teams) 


SELECT 
SportID = Hockey.value('@id[1]', 'int'),
SportName = Hockey.value('@n[1]', 'varchar(20)'),
CatID = Hockey.value('cat[1]/@id', 'int'),
CatName = Hockey.value('cat[1]/@n', 'varchar(20)'),
MatchID = Hockey.value('(cat/m)[1]/@id', 'int'),
MatchName = Hockey.value('(cat/m)[1]/@n', 'varchar(50)'),
MatchMID = Hockey.value('(cat/m)[1]/@mid', 'int'),
MatchDate = Hockey.value('(cat/m)[1]/@dt', 'varchar(20)'),
TypeID = Hockey.value('(cat/m/t)[1]/@id', 'int'),
TypeName = Hockey.value('(cat/m/t)[1]/@n', 'varchar(20)'),
LineID = Hockey.value('(cat/m/t/l)[1]/@id', 'int'),
LineIndex = Hockey.value('(cat/m/t/l)[1]/@i', 'int'),
LineOdds = Hockey.value('(cat/m/t/l)[1]/@o', 'float'),
Teams = Hockey.value('(cat/m/t/l)[1]', 'varchar(20)')

FROM ( 
SELECT CAST(x AS XML)
FROM OPENROWSET(
 BULK 'C:\filer\intertops.xml',
 SINGLE_BLOB) AS T(x)
 ) AS T(x)
CROSS APPLY x.nodes('//result/data/s') AS X(Hockey);

In the attached file I have made a picture over how I would like to have it.. I have only stated a few lines but I want it to import all rows if there are more.

That show how I want to have it enter image description here

This is my first post and I hope you understand what I mean. I have tried to search and find solutions from others here and other places but have not find how to do...

Update 1,

I managed to change the code and to get the data for the first row for each league. So I am wondering what I have to change in order to also get the data for the second "match" in the finish league.

My new code looks like following:

    INSERT INTO dbo.Intertops(SportID, SportName, CatID, CatName, MatchID, MatchName, MatchMID, MatchDate, 
TypeID, TypeName, LineID, LineIndex, LineOdds, Teams)

SELECT
Hockey.value('@id', 'int') as 'SportID',
Hockey.value('@n', 'varchar(20)') as 'SportName',
Hockey.value('cat[1]/@id', 'int') as 'CatID',
Hockey.value('cat[1]/@n', 'varchar(20)') as 'CatName',
HockeyC.value('m[1]/@id', 'int') as 'MatchID',
HockeyC.value('m[1]/@n', 'varchar(50)') as 'MatchName',
HockeyC.value('m[1]/@mid', 'int') as 'MatchMID',
HockeyC.value('m[1]/@dt', 'varchar(20)') as 'MatchDate',
HockeyT.value('t[1]/@id', 'int') as 'TypeID',
HockeyT.value('t[1]/@n', 'varchar(20)') as 'TypeName',
HockeyL.value('l[1]/@id', 'int') as 'LineID',
HockeyL.value('l[1]/@i', 'int') as 'LineIndex',
HockeyL.value('l[1]/@o', 'float') as 'LineOdds',
HockeyL.value('l[1]', 'varchar(20)') as 'Teams'



FROM ( 
SELECT CAST(x AS XML)
FROM OPENROWSET(
 BULK 'C:\filer\intertops.xml',
 SINGLE_BLOB) AS T(x)
 ) AS T(x)
CROSS APPLY x.nodes('//result/data/s') AS X(Hockey)
CROSS APPLY x.nodes('//s/cat') AS Y(HockeyC)
CROSS APPLY x.nodes('//s/cat/m') AS Z(HockeyT)
CROSS APPLY x.nodes('//s/cat/m/t') AS W(HockeyL);

Anyone that can help me to get out all the data?


Solution

  • I modified the query base your version, Is this correct?

    SELECT
    x.Hockey.value('@id', 'int') as 'SportID',
    x.Hockey.value('@n', 'varchar(20)') as 'SportName',
    y.HockeyC.value('@id', 'int') as 'CatID',
    y.HockeyC.value('@n', 'varchar(20)') as 'CatName',
    z.HockeyT.value('@id', 'int') as 'MatchID',
    z.HockeyT.value('@n', 'varchar(50)') as 'MatchName',
    z.HockeyT.value('@mid', 'int') as 'MatchMID',
    z.HockeyT.value('@dt', 'varchar(20)') as 'MatchDate',
    w.HockeyL.value('@id', 'int') as 'TypeID',
    w.HockeyL.value('@n', 'varchar(20)') as 'TypeName',
    l.HockeyK.value('@id', 'int') as 'LineID',
    l.HockeyK.value('@i', 'int') as 'LineIndex',
    l.HockeyK.value('@o', 'float') as 'LineOdds',
    l.HockeyK.value('.', 'varchar(20)') as 'Teams'
    
    FROM ( 
       SELECT CAST(x AS XML)  FROM OPENROWSET(
        BULK 'f:\123.xml',
        SINGLE_BLOB) AS T(x)
     ) AS T(x)
    CROSS APPLY x.nodes('//result/data/s') AS X(Hockey)
    CROSS APPLY x.Hockey.nodes('cat') AS Y(HockeyC)
    --CROSS APPLY x.nodes('//s/cat') AS Y(HockeyC)
    CROSS APPLY y.HockeyC.nodes('m') Z(HockeyT)
    --CROSS APPLY x.nodes('//s/cat/m') AS Z(HockeyT)
    CROSS APPLY z.HockeyT.nodes('t') AS W(HockeyL)
    --CROSS APPLY x.nodes('//s/cat/m/t') AS W(HockeyL);
    CROSS APPLY w.HockeyL.nodes('l') AS L(HockeyK);
    
    SportID     SportName            CatID       CatName              MatchID     MatchName                                          MatchMID    MatchDate            TypeID      TypeName             LineID      LineIndex   LineOdds               Teams
    ----------- -------------------- ----------- -------------------- ----------- -------------------------------------------------- ----------- -------------------- ----------- -------------------- ----------- ----------- ---------------------- --------------------
    16          Ice Hockey           1060        Swedish SHL          1335939     Farjestad BK v Hv71                                804927      2017-03-04T15:00     1           Single Match         23578344    0           3.9                    Draw
    16          Ice Hockey           1060        Swedish SHL          1335939     Farjestad BK v Hv71                                804927      2017-03-04T15:00     1           Single Match         23578343    1           2.35                   Farjestad BK
    16          Ice Hockey           1060        Swedish SHL          1335939     Farjestad BK v Hv71                                804927      2017-03-04T15:00     1           Single Match         23578342    2           2.45                   Hv71
    16          Ice Hockey           1060        Swedish SHL          1335939     Farjestad BK v Hv71                                804927      2017-03-04T15:00     2           Moving Line          23578350    1           3.5                    Farjestad BK (-1.5)
    16          Ice Hockey           1060        Swedish SHL          1335939     Farjestad BK v Hv71                                804927      2017-03-04T15:00     2           Moving Line          23578349    2           1.25                   Hv71 (+1.5)
    16          Ice Hockey           1060        Swedish SHL          1335939     Farjestad BK v Hv71                                804927      2017-03-04T15:00     4           Point Score          23578348    1           1.8                    Game Total OVER 4.5
    16          Ice Hockey           1060        Swedish SHL          1335939     Farjestad BK v Hv71                                804927      2017-03-04T15:00     4           Point Score          23578347    2           2                      Game Total UNDER 4.5
    16          Ice Hockey           1062        Finnish SM-liiga     1335991     Jukurit v Kookoo                                   804993      2017-03-04T15:00     1           Single Match         23579183    0           4                      Draw
    16          Ice Hockey           1062        Finnish SM-liiga     1335991     Jukurit v Kookoo                                   804993      2017-03-04T15:00     1           Single Match         23579182    1           1.8                    Jukurit
    16          Ice Hockey           1062        Finnish SM-liiga     1335991     Jukurit v Kookoo                                   804993      2017-03-04T15:00     1           Single Match         23579181    2           3.5                    Kookoo
    16          Ice Hockey           1062        Finnish SM-liiga     1335991     Jukurit v Kookoo                                   804993      2017-03-04T15:00     2           Moving Line          23579189    1           2.55                   Jukurit (-1.5)
    16          Ice Hockey           1062        Finnish SM-liiga     1335991     Jukurit v Kookoo                                   804993      2017-03-04T15:00     2           Moving Line          23579188    2           1.45                   Kookoo (+1.5)
    16          Ice Hockey           1062        Finnish SM-liiga     1335991     Jukurit v Kookoo                                   804993      2017-03-04T15:00     4           Point Score          23579187    1           1.9                    Game Total OVER 4.5
    16          Ice Hockey           1062        Finnish SM-liiga     1335991     Jukurit v Kookoo                                   804993      2017-03-04T15:00     4           Point Score          23579186    2           1.8                    Game Total UNDER 4.5
    16          Ice Hockey           1062        Finnish SM-liiga     1335996     Ässät Pori v HPK Hämeenlinna                       804998      2017-03-04T15:00     1           Single Match         23579278    0           3.9                    Draw
    16          Ice Hockey           1062        Finnish SM-liiga     1335996     Ässät Pori v HPK Hämeenlinna                       804998      2017-03-04T15:00     1           Single Match         23579277    1           2.25                   Ässät Pori
    16          Ice Hockey           1062        Finnish SM-liiga     1335996     Ässät Pori v HPK Hämeenlinna                       804998      2017-03-04T15:00     1           Single Match         23579276    2           2.65                   HPK Hämeenlinna
    16          Ice Hockey           1062        Finnish SM-liiga     1335996     Ässät Pori v HPK Hämeenlinna                       804998      2017-03-04T15:00     2           Moving Line          23579284    1           3.2                    Ässät Pori (-1.5)
    16          Ice Hockey           1062        Finnish SM-liiga     1335996     Ässät Pori v HPK Hämeenlinna                       804998      2017-03-04T15:00     2           Moving Line          23579283    2           1.3                    HPK Hämeenlinna (+1.
    16          Ice Hockey           1062        Finnish SM-liiga     1335996     Ässät Pori v HPK Hämeenlinna                       804998      2017-03-04T15:00     4           Point Score          23579282    1           1.9                    Game Total OVER 4.5
    16          Ice Hockey           1062        Finnish SM-liiga     1335996     Ässät Pori v HPK Hämeenlinna                       804998      2017-03-04T15:00     4           Point Score          23579281    2           1.8                    Game Total UNDER 4.5