在 SQL Server 的存储过程中解析 XML 并输出

Posted

技术标签:

【中文标题】在 SQL Server 的存储过程中解析 XML 并输出【英文标题】:Parse XML and output in stored procedure in SQL Server 【发布时间】:2019-09-18 20:43:00 【问题描述】:

这是 XML 架构:

<details schemaVersion="1">
  <dataWarning>
    <Locations>
      <Failed_to_Create LocationID="1234">Location<Location_Description>TEST</Location_Description><FacilityID>TEST</FacilityID><Active>Y</Active></Failed_to_Create>
    </Locations>
  </dataWarning>
</details>

如何解析 XML 以便能够在存储过程中显示它?

谢谢!

enter code here

【问题讨论】:

架构在哪里? 【参考方案1】:

使用 Xquery nodes() 方法。它允许访问任何元素或属性。

SQL

DECLARE @xml XML = N'<Failed_to_Create LocationID="">Location
    <Location_Description>TEST</Location_Description>
    <FacilityID>TEST</FacilityID>
    <Active>Y</Active>
</Failed_to_Create>';

SELECT c.value('(./text())[1]', 'VARCHAR(10)') AS [location]
    , c.value('(Location_Description/text())[1]', 'VARCHAR(100)') AS [Location_Description]
    , c.value('(FacilityID/text())[1]', 'VARCHAR(100)') AS [FacilityID]
    , c.value('(Active/text())[1]', 'VARCHAR(100)') AS [Active]
FROM @xml.nodes('/Failed_to_Create') AS t(c);

输出

+------------+----------------------+------------+--------+
|  location  | Location_Description | FacilityID | Active |
+------------+----------------------+------------+--------+
| Location   | TEST                 | TEST       | Y      |
+------------+----------------------+------------+--------+

【讨论】:

您好 Yitzhak,谢谢,但我忘了粘贴整个代码,此外,我需要遍历包含包含此 XML 模式的列详细信息的表。【参考方案2】:

Andrea,你需要更加自律。您有责任提供 DDL 和样本数据群。

SQL

-- DDL and sample data population, start
DECLARE @tbl TABLE  (ID INT IDENTITY(1,1) PRIMARY KEY, Details XML);
INSERT INTO @tbl (Details)
VALUES
(N'<details schemaVersion="1">
  <dataWarning>
    <Locations>
      <Failed_to_Create LocationID="1234">Location<Location_Description>TEST</Location_Description><FacilityID>TEST</FacilityID><Active>Y</Active></Failed_to_Create>
    </Locations>
  </dataWarning>
</details>')
, (N'<details schemaVersion="1">
  <dataWarning>
    <Locations>
      <Failed_to_Create LocationID="5678">Location<Location_Description>Real Description</Location_Description><FacilityID>770</FacilityID><Active>N</Active></Failed_to_Create>
    </Locations>
  </dataWarning>
</details>');
-- DDL and sample data population, end

SELECT c.value('@LocationID', 'VARCHAR(10)') AS [locationID]
    , c.value('(Location_Description/text())[1]', 'VARCHAR(100)') AS [Location_Description]
    , c.value('(FacilityID/text())[1]', 'VARCHAR(100)') AS [FacilityID]
    , c.value('(Active/text())[1]', 'VARCHAR(100)') AS [Active]
FROM @tbl AS tbl
      CROSS APPLY tbl.Details.nodes('/details/dataWarning/Locations/Failed_to_Create') AS t(c);

输出

+------------+----------------------+------------+--------+
| locationID | Location_Description | FacilityID | Active |
+------------+----------------------+------------+--------+
|       1234 | TEST                 | TEST       | Y      |
|       5678 | Real Description     | 770        | N      |
+------------+----------------------+------------+--------+

【讨论】:

以上是关于在 SQL Server 的存储过程中解析 XML 并输出的主要内容,如果未能解决你的问题,请参考以下文章

SQL Server XML转Table

SQL SERVER解析Json

在 Sql Server 中将 xml 文档作为存储过程的参数传递是不是安全?

如何在 SQL Server 2005 中插入值数组?

将 xml 作为参数传递给 SQL Server 中的存储过程

将 xml 字符串参数传递给 SQL Server 存储过程