我有一张带有多个列的表,其中一个是xml
列。 我没有命名空间可用于查询。 所有记录的XML数据结构始终相同。
虚构的数据
create table #temp (id int, name varchar(32), xml_data xml)
insert into #temp values
(1, 'one', '<data><info x="42" y="99">Red</info></data>'),
(2, 'two', '<data><info x="27" y="72">Blue</info></data>'),
(3, 'three', '<data><info x="16" y="51">Green</info></data>'),
(4, 'four', '<data><info x="12" y="37">Yellow</info></data>')
期望的结果
Name Info.x Info.y Info
----- ------- ------- -------
one 42 99 Red
two 27 72 Blue
three 16 51 Green
four 12 37 Yellow
部分工作
select Name, xml_data.query('/data/info/.').value('.', 'varchar(10)') as [Info]
from #temp
它返回Name
和Info
列。我无法弄清如何在不使用命名空间的情况下提取属性值。例如,以下查询会返回错误:
查询1
select Name, xml_data.query('/data/info/@x') as [Info]
from #temp
Msg 2396, Level 16, State 1, Line 12
XQuery [#temp.xml_data.query()]: Attribute may not appear outside of an element
查询2
select Name, xml_data.value('/data/info/@x', 'int') as [Info]
from #temp
Msg 2389, Level 16, State 1, Line 12
XQuery [#temp.xml_data.value()]: 'value()' requires a singleton (or empty sequence), found operand of type 'xdt:untypedAtomic *'
查询 3
select Name, xml_data.query('/data/info/.').value('@x', 'int') as [Info]
from #temp
Msg 2390, Level 16, State 1, Line 9
XQuery [value()]: Top-level attribute nodes are not supported
问题
如何编写查询以返回同一表中的常规列数据和xml
列中的元素+属性值?
.value
时,您需要使用xPath表达式指定单个值。您知道在XML中不会有多个值,但是SQL Server不知道这一点,因此您需要使用[1]
来指定您想要找到的第一个值。 - Mikael Eriksson