在SQL Server中搜索XML列

恩干杜

尝试搜索列数据,其为xml列

[dbo].[HistoryData].[Data].exist('//text()[CONTAINS(., 'Request viewed' )]') = 1

我在“请求”附近收到错误语法错误。我在这里想念什么?请协助。

列数据具有以下内容:

<MessageNotification xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<AttorneyID xsi:nil="true" />
<MatterID xsi:nil="true" />
<NotificationID xsi:nil="true" />
<MessageId>3566913</MessageId>
<ParentMessageGuid xsi:nil="true" />
<MessageGuid>fc54e518-e45d-4564-8b80-2593796d5b</MessageGuid>
<Subject>Firm not registered </Subject>
<MessageDate>2015-06-19T10:05:48.4493646+02:00</MessageDate><Body>

This is just a message

Thank you

</Body>
<ContactPerson> N/A </ContactPerson><ContactDetails xsi:nil="true" />
<PreferredContactMethod>NotApplicable</PreferredContactMethod>
</MessageNotification>
Shnugo

语法错误显然是单引号。

但是我认为还有更多...您想要实现什么?

exist()检查纯存在性并在WHERE子句中大多数情况下使用,以便在执行更昂贵的操作之前减少行数。

试试这个:

DECLARE @dummyTable TABLE(ID INT IDENTITY, YourXML XML);
INSERT INTO @dummyTable VALUES
 (N'<root>
    <a>test</a>
    <a>Some other</a>
   </root>')
,(N'<root>
    <a>no</a>
    <a>Some other</a>
   </root>')
,(N'<root>
    <a>no</a>
    <a>test</a>
   </root>');

SELECT * 
FROM @dummyTable
WHERE YourXML.exist(N'//*[contains(text()[1],"test")]')=1;

结果集不带第2行

更新:使用变量

为了使这个更通用,我建议使用一个变量:

DECLARE @SearchFor NVARCHAR(100)='other';

SELECT * 
FROM @dummyTable
WHERE YourXML.exist(N'//*[contains(text()[1],sql:variable("@SearchFor"))]')=1;

这没有第3行

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章