简体   繁体   English

如何在SQL Server 2008数据库上查询SQL XML中的xml

[英]How to query the xml in SQL XML on a SQL Server 2008 database

I have a SQL Server 2008 database. 我有一个SQL Server 2008数据库。 There is a table called Documents with the following schema: 有一个名为Documents的表,其中包含以下模式:

 Id            int     PK      
 DocumentXml   xml

The xml documents all look something like: xml文档看起来像:

<docroot>
    <name>Some Name</name>
</docroot>

I want to select all the records where the text value of /docroot/name begins with an "S" (case-insenstive). 我想选择/docroot/name的文本值以“S”开头的所有记录(不区分大小写)。

How do I execute this query with the best performance? 如何以最佳性能执行此查询?

I want to select all the records where the text value of /docroot/name begins with an "S" (case-insenstive). 我想选择/docroot/name的文本值以“S”开头的所有记录(不区分大小写)。

Use this XPath expression : 使用此XPath表达式

/*/name[starts-with(translate(.,'S','s'), 's')]

Not sure about the performance - but you can do something like this: 不确定性能 - 但你可以做这样的事情:

SELECT (list of columns)
FROM dbo.Documents
WHERE DocumentXml.value('(/docroot/name)[1]', 'varchar(100)') LIKE 'S%'

If your database collation is case-insensitive, this LIKE operation will be case-insensitive, too. 如果数据库排序规则不区分大小写,则此LIKE操作也不区分大小写。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM