、これは1時間の作業であれば、私はこのようにexist
XMLメソッドを使用します。
DECLARE @Table1 TABLE (
ID INT IDENTITY PRIMARY KEY,
CommentAsXML XML
)
INSERT @Table1 (CommentAsXML)
VALUES (N'<root><item /><item type="Reg">0001</item><item type="Inv">B007</item><item type="Cus">A0001</item><item type="Br">F0001</item></root>')
INSERT @Table1 (CommentAsXML)
VALUES (N'<root><item /><item type="Reg">0005</item><parent><child>B007</child></parent><item type="Br">F0005</item></root>')
INSERT @Table1 (CommentAsXML)
VALUES (N'<root><item /><item type="Reg">0005</item></root>')
-- Following query is searching for B007 within InnerText of all XML elements:
SELECT *
FROM @Table1 t
WHERE t.CommentAsXML.exist('//*[lower-case(text()[1]) eq "b007"]') = 1
結果:
ID CommentAsXML
-- ------------------------------------------------------------------------------------------------------------------------------
1 <root><item type="Reg">0001</item><item type="Inv">B007</item><item type="Cus">A0001</item><item type="Br">F0001</item></root>
2 <root><item type="Reg">0005</item><parent><child>B007</child></parent><item type="Br">F0005</item></root>
また、あなたはXMLのatrributes'値のいくつかのテキストを検索したい場合はその後、次のXQueryを使用することができる:
SELECT *
FROM @Table1 t
WHERE t.CommentAsXML.exist('//@*[lower-case(.) eq "reg"]') = 1
注:どちらの場合も、文字列の詐欺ステント(ex。 "reg")は小文字でなければなりません。
私はすでに存在する方法を見つけました。大文字と小文字が区別されます。大文字と小文字を区別しないで検索する方法はありますか? – vignesh
@vignesh:更新された解決策を見てください: 'lower-case'(または' upper-case')XQuery関数を使うことができます。 –