I have XML Data that I am inserting into a SQL Table
<data>
<racedata>
<race>1</race>
<todays_cls>97</todays_cls>
</racedata>
<stats_data>
<stat type="ALL_WEATHR">
<starts>0</starts>
<wins>0</wins>
<places>0</places>
<shows>0</shows>
<earnings>0.00</earnings>
<paid>0.00</paid>
<roi />
</stat>
<stat type="AT_DST_CRS">
<starts>13</starts>
<wins>4</wins>
<places>1</places>
<shows>0</shows>
<earnings>93448.00</earnings>
<paid>12.00</paid>
<roi>23</roi>
</stat>
</stats_data>
</data>
The racedata inserts fine using SQLBulkCopy. As an example:
bc.DestinationTableName = "racedata";
bc.ColumnMappings.Add("race", "race");
bc.ColumnMappings.Add("todays_cls", "todays_cls");
The issue is when I try to insert the stats_data. When I try to capture the different stat categories like ALL_WEATHR and AT_DST_CRS, I get an error message because the data source and the target table columns have to have the same name. However, ALL_WEATHR and AT_DST_CRS are not column names, they are values. They tell how may wins, places, shows, etc on each type of surface. How would I go about inserting them into a SQL Table?
I think the easiest was to pass the XML into a SQL Server Stored Procedure and shred it there with this:
DECLARE @xml XML=
'<data>
<racedata>
<race>1</race>
<todays_cls>97</todays_cls>
</racedata>
<stats_data>
<stat type="ALL_WEATHR">
<starts>0</starts>
<wins>0</wins>
<places>0</places>
<shows>0</shows>
<earnings>0.00</earnings>
<paid>0.00</paid>
<roi />
</stat>
<stat type="AT_DST_CRS">
<starts>13</starts>
<wins>4</wins>
<places>1</places>
<shows>0</shows>
<earnings>93448.00</earnings>
<paid>12.00</paid>
<roi>23</roi>
</stat>
</stats_data>
</data>';
SELECT RD.value('race[1]','int') AS Race_ID
,RD.value('todays_cls[1]','int') AS Race_todays_cls
,ST.value('@type','varchar(max)') AS Stat_type
,ST.value('(./starts)[1]','int') AS Stat_starts
,ST.value('(./wins)[1]','int') AS Stat_wins
,ST.value('(./places)[1]','int') AS Stat_places
,ST.value('(./shows)[1]','int') AS Stat_shows
,ST.value('(./earnings)[1]','decimal(10,2)') AS Stat_earnings
,ST.value('(./paid)[1]','decimal(10,2)') AS Stat_paid
,ST.value('(./roi)[1]','int') AS Stat_roi
FROM @xml.nodes('data') AS A(DT)
CROSS APPLY DT.nodes('racedata') AS B(RD)
CROSS APPLY DT.nodes('stats_data/stat') AS C(ST)
Within the SP it is easy to store the values from table-like data in one - or better: in many related - table(s)
The result
+---------+-----------------+------------+-------------+-----------+-------------+------------+---------------+-----------+----------+
| Race_ID | Race_todays_cls | Stat_type | Stat_starts | Stat_wins | Stat_places | Stat_shows | Stat_earnings | Stat_paid | Stat_roi |
+---------+-----------------+------------+-------------+-----------+-------------+------------+---------------+-----------+----------+
| 1 | 97 | ALL_WEATHR | 0 | 0 | 0 | 0 | 0.00 | 0.00 | 0 |
+---------+-----------------+------------+-------------+-----------+-------------+------------+---------------+-----------+----------+
| 1 | 97 | AT_DST_CRS | 13 | 4 | 1 | 0 | 93448.00 | 12.00 | 23 |
+---------+-----------------+------------+-------------+-----------+-------------+------------+---------------+-----------+----------+
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.