首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在T中总结xml属性值?

如何在T中总结xml属性值?
EN

Stack Overflow用户
提问于 2022-01-21 01:43:39
回答 1查看 49关注 0票数 -1

我在Server表的数据列中获得了以下XML数据。在选择每一行表时,我想总结所有RecordCount属性的总价值。我该怎么做呢?

代码语言:javascript
复制
<BatchSummary BatchNumber="7" BatchType="SecOwnerTransfer">
    <InterfaceTable TableName="ASR.aps_transferevents" RecordCount="1438" />
    <InterfaceTable TableName="ASR.aps_transferowners" RecordCount="3462" />
    <InterfaceTable TableName="ASR.APS_DeleteTransferEvents" RecordCount="122" />
</BatchSummary>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-01-21 02:14:07

请尝试以下解决方案。

SQL

代码语言:javascript
复制
-- DDL and sample data population, start
DECLARE @tbl TABLE (ID INT IDENTITY PRIMARY KEY, xmldata XML);
INSERT INTO @tbl (xmldata) VALUES
(N'<BatchSummary BatchNumber="7" BatchType="SecOwnerTransfer">
      <InterfaceTable TableName="ASR.aps_transferevents" RecordCount="1438" />
      <InterfaceTable TableName="ASR.aps_transferowners" RecordCount="3462" />
      <InterfaceTable TableName="ASR.APS_DeleteTransferEvents" RecordCount="122" />
    </BatchSummary>'),
(N'<BatchSummary BatchNumber="7" BatchType="SecOwnerTransfer">
      <InterfaceTable TableName="ASR.aps_transferevents" RecordCount="1" />
      <InterfaceTable TableName="ASR.aps_transferowners" RecordCount="3" />
      <InterfaceTable TableName="ASR.APS_DeleteTransferEvents" RecordCount="10" />
    </BatchSummary>');
-- DDL and sample data population, end

SELECT t.*
    , c.value('sum(InterfaceTable/@RecordCount)', 'DECIMAL(12,2)') AS Result
FROM @tbl AS t
    CROSS APPLY xmldata.nodes('/BatchSummary') AS t1(c);

-- to check data type of the RecordCount attribute
SELECT ID
    , TRY_CAST(c.value('.', 'VARCHAR(30)')  AS INT) AS Result
FROM @tbl AS t
    CROSS APPLY xmldata.nodes('/BatchSummary/InterfaceTable/@RecordCount') AS t1(c);

输出

代码语言:javascript
复制
+----+--------+
| ID | Result |
+----+--------+
|  1 |   5022 |
|  2 |     14 |
+----+--------+
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70795434

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档