繁体   English   中英

在SQL Server中搜索XML列

[英]Searching XML column in sql server

尝试搜索列数据,其为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>

语法错误显然是单引号。

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

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行

暂无
暂无

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

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