XML数据和SQL批量复制

时间:2016-03-24 15:46:17

标签: c# sql-server xml

我将XML数据插入到SQL表中

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

使用SQLBulkCopy可以很好地插入racedata。举个例子:

bc.DestinationTableName = "racedata";
bc.ColumnMappings.Add("race", "race");
bc.ColumnMappings.Add("todays_cls", "todays_cls"); 

问题是当我尝试插入stats_data时。当我尝试捕获不同的统计类别,如ALL_WEATHR和AT_DST_CRS时,我收到一条错误消息,因为数据源和目标表列必须具有相同的名称。但是,ALL_WEATHR和AT_DST_CRS不是列名,而是值。他们告诉每种类型的表面如何获胜,地点,表演等。我如何将它们插入SQL表?

1 个答案:

答案 0 :(得分:1)

我认为最简单的方法是将XML传递到SQL Server存储过程并使用此处将其粉碎:

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)

在SP中,很容易将表格式数据中的值存储在一个 - 或更好:在许多相关的表格中

结果

+---------+-----------------+------------+-------------+-----------+-------------+------------+---------------+-----------+----------+
| 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       |
+---------+-----------------+------------+-------------+-----------+-------------+------------+---------------+-----------+----------+