【问题标题】:Importing XML into SQL Server将 XML 导入 SQL Server
【发布时间】:2023-03-16 15:20:01
【问题描述】:

我可以找到很多关于如何将某些类型的 XML 数据导入 SQL Server 2005 的示例。但是我得到了以下格式的数据(使用 ID 重复“行”和“单元格”而不是标签命名等:

<?xml version="1.0"?> <rows>
    <row id='1'>
        <cell id='category'>Simple</cell>
        <cell id='query'>summary</cell>
        <cell id='clientsfound'>6</cell>
        <cell id='eligibleclients'>11</cell>
        <cell id='percentage'>55</cell>
        <cell id='days'>0</cell>
    </row>

    <row id='2'>
        <cell id='category'>Complex</cell>
        <cell id='query'>details</cell>
        <cell id='clientsfound'>4</cell>
        <cell id='eligibleclients'>6</cell>
        <cell id='percentage'>67</cell>
        <cell id='days'>5</cell>
    </row>

    ...
     </rows>

理想情况下,我想将其加载到如下表中:

CREATE TABLE [dbo].[QueryResults](
    [UserString] [varchar](50) NULL,
    [ImportStamp] [timestamp] NULL,
    [RowID] [int] NULL,
    [Category] [nchar](10) NULL,
    [Query] [nchar](10) NULL,
    [ClientsFound] [int] NULL,
    [EligibleClients] [int] NULL,
    [Percentage] [int] NULL,
    [Days] [int] NULL
)

谁能给我一个例子或指向一个在线教程?

【问题讨论】:

    标签: sql sql-server xml tsql


    【解决方案1】:

    xml 内部应该是 "" 而不是 ',不是吗?

    无论如何,您可以本地解析 XML 数据类型。 sp_xml_preparedocument 坦率地说是危险的,因为内存使用的开销。

    DECLARE @foo XML;
    
    SET @foo = N'<?xml version="1.0"?>
    <rows>
        <row id="1">
            <cell id="category">Simple</cell>
            <cell id="query">summary</cell>
            <cell id="clientsfound">6</cell>
            <cell id="eligibleclients">11</cell>
            <cell id="percentage">55</cell>
            <cell id="days">0</cell>
        </row>
        <row id="2">
            <cell id="category">Complex</cell>
            <cell id="query">details</cell>
            <cell id="clientsfound">4</cell>
            <cell id="eligibleclients">6</cell>
            <cell id="percentage">67</cell>
            <cell id="days">5</cell>
        </row>
    </rows>';
    
    SELECT
        x.item.value('@id', 'int') AS RowID,
        y.item.value('(./cell[@id="category"])[1]', 'nchar(10)') AS category,
        y.item.value('(./cell[@id="query"])[1]', 'nchar(10)') AS query,
        y.item.value('(./cell[@id="clientsfound"])[1]', 'int') AS clientsfound,
        y.item.value('(./cell[@id="eligibleclients"])[1]', 'int') AS eligibleclients,
        y.item.value('(./cell[@id="percentage"])[1]', 'int') AS percentage,
        y.item.value('(./cell[@id="days"])[1]', 'int') AS days
    FROM
        @foo.nodes('/rows/row') x(item)
        CROSS APPLY
        x.item.nodes('.') AS y(item)
    

    【讨论】:

      【解决方案2】:

      您可以使用 OPENXML 和 XQUERY 来完成。

      DECLARE @XMLdoc XML
      DECLARE @idoc int
      SELECT @XMLdoc = '<?xml version="1.0"?>
        <rows>
          <row id="1">
              <cell id="category">Simple</cell>
              <cell id="query">summary</cell>
              <cell id="clientsfound">6</cell>
              <cell id="eligibleclients">11</cell>
              <cell id="percentage">55</cell>
              <cell id="days">0</cell>
          </row>
          <row id="2">
              <cell id="category">Complex</cell>
              <cell id="query">details</cell>
              <cell id="clientsfound">4</cell>
              <cell id="eligibleclients">6</cell>
              <cell id="percentage">67</cell>
              <cell id="days">5</cell>
          </row>
        </rows>'
      
      
      -- Create an internal representation of the XML document.
      EXEC sp_xml_preparedocument @idoc OUTPUT, @XMLDoc
      
      INSERT INTO QueryResults (RowID,Category,Query,ClientsFound,EligibleClients,Percentage,Days)
      SELECT  id,
              overflow.value('(/row/cell[@id="category"])[1]', 'nchar(10)'),
              overflow.value('(/row/cell[@id="query"])[1]', 'nchar(10)'),
              overflow.value('(/row/cell[@id="clientsfound"])[1]', 'int'),
              overflow.value('(/row/cell[@id="eligibleclients"])[1]', 'int'),
              overflow.value('(/row/cell[@id="percentage"])[1]', 'int'),
              overflow.value('(/row/cell[@id="days"])[1]', 'int')
      FROM OPENXML (@idoc, '/rows/row',10)
      WITH (id int '@id',
          overflow xml '@mp:xmltext' --the row xml node
      )
      
      -- Release resources allocated for the XML document.
      EXEC sp_xml_removedocument @idoc
      
      SELECT * FROM QueryResults
      

      结果:

      UserString  ImportStamp        RowID  Category  Query    ClientsFound EligibleClients Percentage  Days
      ----------- ------------------ ------ --------- -------- ------------ --------------- ----------- ----
      NULL        0x000000000000C1CA 1      Simple    summary  6            11              55          0
      NULL        0x000000000000C1CB 2      Complex   details  4            6               67          5
      

      我不确定你想在“UserString”中填充什么,但你可以稍后解决。

      希望这能为您的问题提供合适的解决方案。

      -- 对不起 gbn,您对 sp_xml_preparedocument 的看法可能是正确的。我只是从我们与 Microsoft SDC 团队合作的一个项目中获得的一些类似的存储过程中采用了这种方法,所以认为它是安全的。无论如何,您的方法可能更清洁。

      【讨论】:

      • SQL 2005 中无需使用 sp_xml_preparedocument
      • XML 处理更改是 SQL Server 2005 更好的功能之一... :-)
      猜你喜欢
      • 2013-05-26
      • 2018-05-06
      • 1970-01-01
      • 1970-01-01
      • 2016-10-29
      • 2011-09-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多