如果我有:
<quotes>
<quote>
<name>john</name>
<content>something or other</content>
</quote>
<quote>
<name>mary</name>
<content>random stuff</content>
</quote>
</quotes>
如何使用 T-SQL 获取元素名称“名称”和“内容”的列表?
到目前为止我得到的最好的是:
declare @xml xml
set @xml = ...
select r.value('quotes/name()[1]', 'nvarchar(20)' as ElementName
from @xml.nodes('/quotes') as records(r)
但是,当然,我无法让它工作。
最佳答案
实际上,抱歉,我得到的最好的是:
select distinct r.value('fn:local-name(.)', 'nvarchar(50)') as t
FROM
@xml.nodes('//quotes/*/*') AS records(r)
猜猜我回答了我自己的问题......
关于sql - 使用 t-SQL 检索 XML 元素名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3180834/