我们想在sql中通过xml路径进行查询。我们应该怎么做?

时间:2013-02-02 10:07:06

标签: sql sql-server sql-server-2008

  

可能重复:
  SQL Server - XQuery for XML

我们有一个类似于此的xml列:

ROW1:

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

ROW2:

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

...

我们希望选择具有f="value1"

的行

1 个答案:

答案 0 :(得分:2)

SQL Fiddle

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

<强> Results

|                                                                                                              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"/> |