使用属性将 XML 导入 SQL 服务器 Table

Import XML to SQL Server Table with attributes

我需要帮助将数据从 XML 导入到 SQL 服务器 table。 我只设法为第一场比赛导入一行,需要帮助以了解如何继续导入所有 "rows"..

我想插入的数据看起来像这样,并保存在我光盘上的一个文件中,但稍后我想从 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>

我设法导入了 "first row" 但没有导入上面代码中的其他两行。我的代码如下所示:

    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);

在附件中,我画了一张图片,说明我想要它的方式。我只说明了几行,但如果有更多行,我希望它导入所有行。

这表明我多么想拥有它

这是我的第一个 post,我希望你明白我的意思。我试图在这里和其他地方搜索并找到其他人的解决方案,但没有找到如何做...

更新 1,

我设法更改了代码并获取了每个联赛第一行的数据。所以我想知道我必须改变什么才能在完成联赛中获得第二个 "match" 的数据。

我的新代码如下所示:

    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);

谁能帮我把所有数据都拿出来?

我根据你的版本修改了查询,是否正确?

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:3.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