我有这段T-SQL中的XML:
<Elements>
<Element>
<Index>1</Index>
<Type>A</Type>
<Code>AB</Code>
<Time>1900-01-01T10:21:00</Time>
</Element>
<Element>
<Index>2</Index>
<Type>M</Type>
<Code>AL</Code>
<Time>1900-01-01T10:22:00</Time>
</Element>
</Elements>
我想将其作为表格检索出来:
Index FieldName FieldValue
-------- ------------ ----------
1 Index 1
1 Type A
1 Code AB
1 Time 1900-01-01T10:21:00
2 Index 2
2 Type M
2 Code AL
2 Time 1900-01-01T10:22:00
当然,我在这里寻求的是将元素节点转换为行,但一次只能获取字段值或索引,无法同时获取更多信息...
select
-- r.value('.[1]', 'nvarchar(10)') Value,
-- r.value('fn:local-name(.)', 'nvarchar(50)') FieldName
r.value('Index[1]', 'nvarchar(10)') f,
r.value('./node()[fn:local-name(.)]', 'nvarchar(10)') v
from
@content.nodes('/Elements/*') as records(r)