简体   繁体   English

如何从 sql 服务器中的 XML 生成特定表?

[英]How Can I generate specific table from XML in sql server?

I have a xml document.我有一个 xml 文档。 I want to generate table with this xml.我想用这个 xml 生成表。 Here is my xml data这是我的 xml 数据

Declare @Result XML
SET @Result='
<General>
  <Customer>
    <model_year>2011</model_year>
    <vehicle_id>1</vehicle_id>
    <customer_requests>
      <request>
        <definition>I want to new car tyre</definition>
      </request>
      <request>
        <definition>I want to new headlight</definition>
      </request>
    </customer_requests>
    <mileage>34000</mileage>
  </Customer>
  <Customer>
    <model_year>2012</model_year>
    <vehicle_id>2</vehicle_id>
    <customer_requests>
      <request>
        <definition>I want to general maintenance</definition>
      </request>
    </customer_requests>
    <mileage>35000</mileage>
  </Customer>
</General>
'

This is sample output这是样品 output

model_year型号年份 vehicle_id车辆编号 request要求 mileage里程
2011 2011 1 1 I want to new car tyre,I want to new headlight我要换新轮胎,我要换大灯 34000 34000
2012 2012 2 2 I want to general maintenance我要一般保养 35000 35000

How can I generate table from XML data like sample output with tsql?如何从 XML 数据(如示例 output 和 tsql)生成表?

Please try the following solution.请尝试以下解决方案。

It is using XQuery's FLWOR expression to concatenate multiple <definition> tag values into one string.它使用 XQuery 的 FLWOR 表达式将多个<definition>标记值连接到一个字符串中。

SQL SQL

DECLARE @Result XML = 
N'<General>
  <Customer>
    <model_year>2011</model_year>
    <vehicle_id>1</vehicle_id>
    <customer_requests>
      <request>
        <definition>I want to new car tyre</definition>
      </request>
      <request>
        <definition>I want to new headlight</definition>
      </request>
    </customer_requests>
    <mileage>34000</mileage>
  </Customer>
  <Customer>
    <model_year>2012</model_year>
    <vehicle_id>2</vehicle_id>
    <customer_requests>
      <request>
        <definition>I want to general maintenance</definition>
      </request>
    </customer_requests>
    <mileage>35000</mileage>
  </Customer>
</General>';

SELECT c.value('(model_year/text())[1]', 'CHAR(4)') AS model_year
    ,  c.value('(vehicle_id/text())[1]', 'INT') AS vehicle_id
    ,  c.query('for $x in customer_requests/request/definition
                return if ($x is (customer_requests/request[last()]/definition)[1]) then data($x)
                        else concat($x/text()[1], ",")')
        .value('text()[1]', 'VARCHAR(MAX)') AS request
    ,  c.value('(mileage/text())[1]', 'INT') AS mileage
FROM @Result.nodes('/General/Customer') AS t(c);

Output Output

+------------+------------+-------------------------------------------------+---------+
| model_year | vehicle_id |                     request                     | mileage |
+------------+------------+-------------------------------------------------+---------+
|       2011 |          1 | I want to new car tyre, I want to new headlight |   34000 |
|       2012 |          2 | I want to general maintenance                   |   35000 |
+------------+------------+-------------------------------------------------+---------+

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

相关问题 如何编写查询以从SQL Server表生成XML文件 - How to write query to generate XML file from SQL Server table SQL Server &amp; XML:如何从 sql 表中获取 xml 属性? - SQL Server & XML: how can I get an xml attribute from sql table? 如何为SQL Server表中的所有行返回XML列中特定属性的值? - How can I return the value of a specific attribute in an XML column for all rows in a SQL Server table? 如何从 SQL Server 中特定数据库的表中获取所有列名? - How can I get all column names from a table from a specific database in SQL Server? 如何在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 can I update an SQL database table from an XML file? 如何在 SQL Server 2000 中生成一个填充日期的临时表? - How can I generate a temporary table filled with dates in SQL Server 2000? SQL Server:如何从带有前缀的表中选择所有内容? - SQL Server: How can I select everything from a table with a prefix? 如何从此Sql Server表中删除重复项? - How can i remove duplicates from this Sql Server Table? 如何从 SQL Server 中的表中获取列名? - How can I get column names from a table in SQL Server?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM