1

可能重复:
SQL Server - XQuery for XML

我们有一个类似这样的 xml 列:

第 1 行:

<a f="value1" b="value2" c="value3" d="value4"  />
<a f="value5" b="value6" c="value7" d="value8" Case="value9"  />

第2行:

<a f="value1" b="value15" c="value16" d="value17"  />
<a f="value10" b="value11" c="value12" d="value13" Case="value14"  />

...

我们想要选择具有f="value1".

4

1 回答 1

2

SQL小提琴

MS SQL Server 2008 架构设置

create table YourTable
(
  XMLColumn xml
)

insert into YourTable values('<a f="value1" b="value2" c="value3" d="value4"  />
<a f="value5" b="value6" c="value7" d="value8" Case="value9"  />')

insert into YourTable values('<a f="value1" b="value15" c="value16" d="value17"  />
<a f="value10" b="value11" c="value12" d="value13" Case="value14"  />')

查询 1

select *
from YourTable
where XMLColumn.exist('a[@f = "value1"]') = 1

结果

|                                                                                                              XMLCOLUMN |
--------------------------------------------------------------------------------------------------------------------------
|         <a f="value1" b="value2" c="value3" d="value4"/><a f="value5" b="value6" c="value7" d="value8" Case="value9"/> |
| <a f="value1" b="value15" c="value16" d="value17"/><a f="value10" b="value11" c="value12" d="value13" Case="value14"/> |
于 2013-02-02T10:11:30.083 回答