1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
| -- ============================================================-- sample code on using the SQL Server xml method exist()-- several samples shown-- Donabel Santos-- ============================================================DECLARE @xmlSnippet XMLDECLARE @id SMALLINTDECLARE @value VARCHAR(20)SET @xmlSnippet ='<ninjaElement id="1">SQL Server Ninja</ninjaElement><ninjaElement id="2">SharePoint Ninja</ninjaElement><ninjaElement id="3">ASP.NET Ninja</ninjaElement>'-- this is what we will look forSET @id = 2SET @value ='SQL Server Ninja'-- note exist() will return only either :-- 1 (true) or 0 (false)-- check if a node called ninjaElement exists-- at any level in the XML snippetSELECT @xml.exist('//ninjaElement')-- check if a node called bar existsSELECT @xml.exist('//bar')-- check if attribute id exists anywhereSELECT @xml.exist('//@id')-- check if attribute id exists within a ninjaElement tagSELECT @xml.exist('//ninjaElement[@id]')-- check if the id attribute equals to what we saved-- in the @id variableSELECT @xml.exist('/ninjaElement[@id=sql:variable("@id")]')-- check if the node text equals to what-- we saved in the @value variableSELECT @xml.exist('/ninjaElement1') |
SQL XMl Using SQL Server XMl Exists() Function to fing Element in XML
Thursday, October 27, 2011
Subscribe to:
Post Comments (Atom)
loading..
No comments:
Post a Comment
Post Your Comment...