[英]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.