我需要将数据从 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);
任何人都可以帮助我获取所有数据吗?