[英]Searching XML column in sql server
Trying to search column Data, its an xml column 尝试搜索列数据,其为xml列
[dbo].[HistoryData].[Data].exist('//text()[CONTAINS(., 'Request viewed' )]') = 1
I get error Incorrect syntax near 'Request'. 我在“请求”附近收到错误语法错误。 What am i missing here?
我在这里想念什么? kindly assist.
请协助。
Column Data has this: 列数据具有以下内容:
<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>
The syntax error are the single quotes obviously. 语法错误显然是单引号。
But I think there is more around... What are you trying to achieve? 但是我认为还有更多...您想要实现什么?
The exist()
is checking for the pure existance and in most cases used within a WHERE
clause in order to reduce the rows before a more expensive operations. exist()
检查纯存在性,并在大多数情况下在WHERE
子句中使用,以便在执行更昂贵的操作之前减少行数。
Try this: 尝试这个:
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;
The result set comes without line 2 结果集不包含第2行
In order to get this more generic I'd suggest to use a variable: 为了使这个更通用,我建议使用一个变量:
DECLARE @SearchFor NVARCHAR(100)='other';
SELECT *
FROM @dummyTable
WHERE YourXML.exist(N'//*[contains(text()[1],sql:variable("@SearchFor"))]')=1;
This comes without line 3 这没有第3行
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.