希望你们都做得很好。
我需要将XML-feed从网站导入我的SQL Server数据库。我对XML知之甚少。
饲料结构有点复杂。以下是该文件的示例:
<line_feed>
<FeedTime>1279519582927</FeedTime>
<lastContest>4103839</lastContest>
<lastGame>58629754</lastGame>
<events>
<event>
<event_datetimeGMT>2010-07-19 21:30</event_datetimeGMT>
<gamenumber>174087393</gamenumber>
<sporttype>Tennis</sporttype>
<league>abc</league>
<participants>
<participant>
<participant_name>R. Ram</participant_name>
<contestantnum>4303</contestantnum>
<rotnum>4303</rotnum>
<visiting_home_draw>Visiting</visiting_home_draw>
</participant>
<participant>
<participant_name>K. Beck</participant_name>
<contestantnum>4304</contestantnum>
<rotnum>4304</rotnum>
<visiting_home_draw>Home</visiting_home_draw>
</participant>
</participants>
<periods>
<period>
<period_number>0</period_number>
<period_description>Game</period_description>
<periodcutoff_datetimeGMT>2010-07-19 21:30</periodcutoff_datetimeGMT>
<period_status>I</period_status>
<period_update>open</period_update>
<spread_maximum>250</spread_maximum>
<moneyline_maximum>1500</moneyline_maximum>
<total_maximum>250</total_maximum>
<moneyline>
<moneyline_visiting>135</moneyline_visiting>
<moneyline_home>-146</moneyline_home>
</moneyline>
</period>
<period>
<period_number>0</period_number>
<period_description>Game</period_description>
<periodcutoff_datetimeGMT>2010-07-19 21:30</periodcutoff_datetimeGMT>
<period_status>I</period_status>
<period_update>open</period_update>
<spread_maximum>250</spread_maximum>
<moneyline_maximum>250</moneyline_maximum>
<total_maximum>250</total_maximum>
<spread>
<spread_visiting>2</spread_visiting>
<spread_adjust_visiting>101</spread_adjust_visiting>
<spread_home>-2</spread_home>
<spread_adjust_home>-118</spread_adjust_home>
</spread>
<total>
<total_points>22.5</total_points>
<over_adjust>-108</over_adjust>
<under_adjust>-108</under_adjust>
</total>
</period>
<period>
<period_number>1</period_number>
<period_description>1st Set</period_description>
<periodcutoff_datetimeGMT>2010-07-19 21:30</periodcutoff_datetimeGMT>
<period_status>I</period_status>
<period_update>open</period_update>
<spread_maximum>5000</spread_maximum>
<moneyline_maximum>250</moneyline_maximum>
<total_maximum>5000</total_maximum>
<moneyline>
<moneyline_visiting>114</moneyline_visiting>
<moneyline_home>-133</moneyline_home>
</moneyline>
</period>
</periods>
</event>
</events>
</line_feed>
您可以帮我解决一下,如何将该数据从该Feed加载到SQL Server。
请问我是否需要了解此方案的任何其他详细信息。等待你的回应。
谢谢你, PRASHANT
答案 0 :(得分:1)
这完全取决于您要从Feed中提取的内容....
我假设您将XML存储在SQL Server变量中,如下所示:
DECLARE @input XML
此查询将枚举所有<participants>
个节点并提取有关单个参与者的所有信息 - 您可以使用此信息将这些参与者插入表中:
SELECT
Feed.Participant.value('(participant_name)[1]', 'varchar(50)') AS ParticipantName,
Feed.Participant.value('(contestantnum)[1]', 'int') AS ContestantNum,
Feed.Participant.value('(rotnum)[1]', 'int') AS RotNum,
Feed.Participant.value('(visiting_home_draw)[1]', 'varchar(50)') AS VisitingHome
FROM
@input.nodes('/line_feed/events/event/participants/participant') AS Feed(Participant)
输出:
ParticipantName ContestantNum RotNum VisitingHome
R. Ram 4303 4303 Visiting
K. Beck 4304 4304 Home
第二个查询枚举了Feed中的<period>
个节点 - 再次提取了一些可用于存储在数据库表中的信息:
SELECT
Feed.Period.value('(period_number)[1]', 'int') AS PeriodNumber,
Feed.Period.value('(period_description)[1]', 'varchar(50)') AS PeriodDescription,
Feed.Period.value('(period_status)[1]', 'varchar(50)') AS Status,
Feed.Period.value('(period_update)[1]', 'varchar(50)') AS Update,
Feed.Period.value('(moneyline_maximum)[1]', 'decimal(18,4)') AS MoneylineMaximum
FROM
@input.nodes('/line_feed/events/event/periods/period') AS Feed(Period)
输出:
PeriodNumber PeriodDescription Status Update MoneylineMaximum
0 Game I open 1500.0000
0 Game I open 250.0000
1 1st Set I open 250.0000
答案 1 :(得分:0)
如果您的XML格式正确,则可以将其存储在xml
类型变量中。然后,您可以使用XPath从中读取字段:
declare @xml xml
set @xml = '
<line_feed>
<PinnacleFeedTime>1279519582927</PinnacleFeedTime>
...
'
select @xml.value('(line_feed/events/event/sporttype)[1]', 'VARCHAR(8000)')
这将打印Tennis
。