简体   繁体   English

SQL 服务器解析 XML 到表 - 同名的多个节点和第一个节点应该是表列

[英]SQL Server parse XML to table - multiple node with the same name and first node should be table columns

I have a source xml like below and I want to convert to SQL table.我有一个源 xml 如下所示,我想转换为 SQL 表。 First "tr" node should be columns for the table and remaining are rows.第一个“tr”节点应该是表的列,其余的是行。

<table>
      <tbody>
      <tr>
        <td>Description</td>
        <td>Value</td>
        <td>Reference</td>
        <td>Category</td>
        <td>Review</td>
      </tr>
      <tr>
        <td>Process Param</td>
        <td>|ABCD|</td>
        <td>Step1</td>
        <td>Process</td>
        <td>False</td>
      </tr>
      <tr>
        <td>Config Param</td>
        <td>|EFGH|</td>
        <td>Step2</td>
        <td>Config</td>
        <td>True</td>
      </tr>
      <tr>
        <td>Process Param</td>
        <td>|IJKL|</td>
        <td>Step3</td>
        <td>Process</td>
        <td>False</td>
      </tr>
    </tbody>
  </table>

Expected SQL output: Output预期 SQL output: Output

You can extract data from your html/xml structure using the nodes method provided for the xml data type (more info here ).您可以使用为 xml 数据类型提供的nodes方法从您的 html/xml 结构中提取数据( 此处有更多信息)。

Here is a simple query that should put you on the right track:这是一个简单的查询,应该让您走上正轨:

select 
     Tbl.Col.value('td[1]', 'varchar(50)')  
    ,Tbl.Col.value('td[2]', 'varchar(50)')  
    ,Tbl.Col.value('td[3]', 'varchar(50)')  
    ,Tbl.Col.value('td[4]', 'varchar(50)')  
    ,Tbl.Col.value('td[5]', 'varchar(50)')  
from @x.nodes('//tr') Tbl(Col)  

where @x is a xml variable containing your xml:其中@x 是一个 xml 变量,其中包含您的 xml:

declare @x xml = '<table>...

The problem is that the first row (that contains column headers) is returned inside the dataset:问题是第一行(包含列标题)在数据集中返回:

在此处输入图像描述

If you want to use values in the first row as column names you'll have to write dynamic xml.如果要将第一行中的值用作列名,则必须编写动态 xml。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在SQL Server表中解析XML - Parse XML in SQL Server table 如何在SQL Server的同一张表中显示XML节点内容从一列到另一列? - How can I display XML node content from one column into another column on the same table in SQL Server? 如何从具有多个在父节点下相同的节点的xml中提取数据到sql中的表 - how to extract data from xml having multiple nodes which are same under a parent node into a table in sql 当多个节点/元素具有相同名称时,SQL Server在表中查询值的多个XML记录 - SQL Server, query multiple XML records in a table for a value, when multiple nodes/elements have the same name SQL Server连接具有相同列名称的不同表 - SQL Server Join Different table with same columns name 从表中节点内具有相同名称的 xml 节点插入值 - Inserting values from an xml node that has the same name inside the node in a table SQL Server删除触发器以更新同一表中的多个列 - SQL Server Delete Trigger To Update Multiple Columns in Same Table SQL Server-比较同一表中differentnet行中的多个列 - SQL Server - Compare on multiple columns in differnet rows within same table SQL Server:查找同一表中多个列的中位数 - SQL Server : Finding medians of multiple columns in the same table 如何解析SQL Server表中的XML数据 - How to parse XML data in SQL server table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM