使用属性将XML导入SQL Server表

时间:2017-03-03 23:17:59

标签: sql-server sql-server-2016 sql-server-2016-localdb

我需要帮助将数据从XML导入到SQL Server表。 我只设法为第一场比赛导入一行,并需要帮助以了解如何继续获取所有"行"进口..

我要插入的数据看起来像这样,并保存在我的光盘上的文件中,但我稍后会想要从网址获取该文件:

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

我设法导入"第一行"但不是上面代码中的其他两行。我的代码如下所示:

{{1}}

在附件中,我已经对我想要的方式进行了描述。我只说了几行,但我希望它能导入所有行,如果还有更多行。

这表明我想拥有它 enter image description here

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

更新1,

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

我的新代码如下:

{{1}}

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

1 个答案:

答案 0 :(得分: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          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