Count the number of sub tags from XML in SQL

半世苍凉 提交于 2019-12-06 13:26:41

I'd do this with xpath:

select @MainXML.query('count(root/a)')

sql fiddle demo

If you're using mssql server then you can take advantage of the OPENXML support. You can get the count of the a nodes by using a SELECT on the parsed XML:

Declare @MainXML XML =
    '<root>
    <a>JJ</a>
    <a>KK</a>
    </root>'

SELECT COUNT(MainXML.A.value('.', 'VARCHAR(100)')) AS Cnt
FROM @MainXML.nodes('root/a') MainXML(A)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!