Search XML with a LIKE or similar full search operation

て烟熏妆下的殇ゞ 提交于 2019-12-04 15:36:04

问题


I want to search an XML valued column to see if a contains a string. I don't know the schema, I want to know if the string is contained anywhere at all. I don't know if XPATH would work in this situation.

The equivalent of

Select s.Name, ts.ValueXML from table t (nolock) 
join table2 ts (nolock) on t.key_Id = ts.key_Id
join table3 s (nolock) on ts.key_Id=s.key_Id
where s.Name like '%Lab%' and ts.ValueXML  like '%PreviewDateRange%'

ERROR: Argument data type xml is invalid for argument 1 of like function.

relevant ts Table columns

ValueXml (XML(.), null)

The item I'm searching for should be an attribute. So if the above isn't possible, then anything containing that attribute would be a good alternative.


回答1:


The simplest (but definitely not the fastest to execute) way would be to cast your column to nvarchar(max) before passing it to like:

cast(ValueXml as nvarchar(max)) like '%PreviewDateRange%'



回答2:


There you go:

SELECT *
FROM MyTable
WHERE MyXmlMetadataColumn.exist('//*/text()[contains(upper-case(.),upper-case("MySearchTerm"))]') = 1

This seems to work fine for me and also does not search the XML tag names as the solution provided by "dasblinkenlight".

I don't know how performant it is though.



来源:https://stackoverflow.com/questions/10471537/search-xml-with-a-like-or-similar-full-search-operation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!