0

我需要将数据从 XML 导入 SQL Server 表的帮助。我只为第一场比赛导入了一行,需要帮助来了解如何继续导入所有“行”。

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

我设法从上面的代码中导入了“第一行”,但没有导入其他两行。我的代码如下所示:

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

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

这表明我想拥有它 在此处输入图像描述

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

更新1,

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

我的新代码如下所示:

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

任何人都可以帮助我获取所有数据吗?

4

1 回答 1

0

我修改了查询基础您的版本,这是正确的吗?

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 冰球 1060 瑞典 SHL 1335939 Farjestad BK v Hv71 804927 2017-03-04T15:00 1 单场比赛 23578344 0 3.9 平局
16 冰球 1060 瑞典 SHL 1335939 Farjestad BK v Hv71 804927 2017-03-04T15:00 1 单场比赛 23578343 1 2.35 Farjestad BK
16 冰球 1060 瑞典 SHL 1335939 Farjestad BK v Hv71 804927 2017-03-04T15:00 1 单场比赛 23578342 2 2.45 Hv71
16 冰球 1060 瑞典 SHL 1335939 Farjestad BK v Hv71 804927 2017-03-04T15:00 2 移动线 23578350 1 3.5 Farjestad BK (-1.5)
16 冰球 1060 瑞典 SHL 1335939 Farjestad BK v Hv71 804927 2017-03-04T15:00 2 移动线 23578349 2 1.25 Hv71 (+1.5)
16 冰球 1060 瑞典 SHL 1335939 Farjestad BK v Hv71 804927 2017-03-04T15:00 4 分 23578348 1 1.8 比赛总分超过 4.5
16 冰球 1060 瑞典 SHL 1335939 Farjestad BK v Hv71 804927 2017-03-04T15:00 4 分得分 23578347 2 2 比赛总分低于 ​​4.5
16 冰球 1062 芬兰 SM-liiga 1335991 Jukurit v Kookoo 804993 2017-03-04T15:00 1 单场比赛 23579183 0 4 平局
16 冰球 1062 芬兰 SM-liiga 1335991 Jukurit v Kookoo 804993 2017-03-04T15:00 1 单场比赛 23579182 1 1.8 Jukurit
16 冰球 1062 芬兰 SM-liiga 1335991 Jukurit v Kookoo 804993 2017-03-04T15:00 1 单场比赛 23579181 2 3.5 Kookoo
16 冰球 1062 芬兰 SM-liiga 1335991 Jukurit v Kookoo 804993 2017-03-04T15:00 2 Moving Line 23579189 1 2.55 Jukurit (-1.5)
16 冰球 1062 芬兰 SM-liiga 1335991 Jukurit v Kookoo 804993 2017-03-04T15:00 2 Moving Line 23579188 2 1.45 Kookoo (+1.5)
16 冰球 1062 芬兰 SM-liiga 1335991 Jukurit v Kookoo 804993 2017-03-04T15:00 4 分 23579187 1 1.9 比赛总分超过 4.5
16 冰球 1062 芬兰 SM-liiga 1335991 Jukurit v Kookoo 804993 2017-03-04T15:00 4 分 23579186 2 1.8 比赛总分低于 ​​4.5
16 冰球 1062 芬兰 SM-liiga 1335996 Ässät Pori v HPK Hämeenlinna 804998 2017-03-04T15:00 1 单场比赛 23579278 0 3.9 平局
16 冰球 1062 芬兰 SM-liiga 1335996 Ässät Pori v HPK Hämeenlinna 804998 2017-03-04T15:00 1 单场比赛 23579277 1 2.25 Ässät Pori
16 冰球 1062 芬兰 SM-liiga 1335996 Ässät Pori v HPK Hämeenlinna 804998 2017-03-04T15:00 1 单场比赛 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 冰球 1062 芬兰 SM-liiga 1335996 Ässät Pori v HPK Hämeenlinna 804998 2017-03-04T15:00 4 分 23579282 1 1.9 比赛总分超过 4.5
16 冰球 1062 芬兰 SM-liiga 1335996 Ässät Pori v HPK Hämeenlinna 804998 2017-03-04T15:00 4 分 23579281 2 1.8 比赛总分低于 ​​4.5

于 2017-03-05T01:23:08.660 回答