You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/t-sql/xml/exist-method-xml-data-type.md
+19-20Lines changed: 19 additions & 20 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -29,8 +29,7 @@ ms.author: genemi
29
29
30
30
## Syntax
31
31
32
-
```
33
-
32
+
```syntaxsql
34
33
exist (XQuery)
35
34
```
36
35
@@ -45,10 +44,10 @@ exist (XQuery)
45
44
> [!NOTE]
46
45
> The **exist()** method returns 1 for the XQuery expression that returns a nonempty result. If you specify the **true()** or **false()** functions inside the **exist()** method, the **exist()** method will return 1, because the functions **true()** and **false()** return Boolean True and False, respectively. That is, they return a nonempty result). Therefore, **exist()** will return 1 (True), as shown in the following example:
47
46
48
-
```
49
-
declare @x xml;
50
-
set @x='';
51
-
select @x.exist('true()');
47
+
```sql
48
+
DECLARE @x XML;
49
+
SET @x='';
50
+
SELECT @x.exist('true()');
52
51
```
53
52
54
53
## Examples
@@ -57,12 +56,12 @@ select @x.exist('true()');
57
56
### Example: Specifying the exist() method against an xml type variable
58
57
In the following example, @x is an **xml** type variable (untyped xml) and @f is an integer type variable that stores the value returned by the **exist()** method. The **exist()** method returns True (1) if the date value stored in the XML instance is `2002-01-01`.
59
58
60
-
```
61
-
declare @x xml;
62
-
declare @f bit;
63
-
set @x = '<root Somedate = "2002-01-01Z"/>';
64
-
set @f = @x.exist('/root[(@Somedate cast as xs:date?) eq xs:date("2002-01-01Z")]');
65
-
select @f;
59
+
```sql
60
+
DECLARE @x XML;
61
+
DECLARE @f BIT;
62
+
SET @x ='<root Somedate = "2002-01-01Z"/>';
63
+
SET @f = @x.exist('/root[(@Somedate cast as xs:date?) eq xs:date("2002-01-01Z")]');
64
+
SELECT @f;
66
65
```
67
66
68
67
In comparing dates in the **exist()** method, note the following:
@@ -75,9 +74,9 @@ select @f;
75
74
76
75
The following example is similar to the previous one, except it has a <`Somedate`> element.
77
76
78
-
```
79
-
DECLARE @x xml;
80
-
DECLARE @f bit;
77
+
```sql
78
+
DECLARE @x XML;
79
+
DECLARE @f BIT;
81
80
SET @x ='<Somedate>2002-01-01Z</Somedate>';
82
81
SET @f = @x.exist('/Somedate[(text()[1] cast as xs:date ?) = xs:date("2002-01-01Z") ]')
83
82
SELECT @f;
@@ -94,13 +93,13 @@ SELECT @f;
94
93
95
94
The **exist()** method specified against the @x variable returns 1 (True) if the manufacturing instructions document includes a <`Location`> element that has `LocationID=50`. Otherwise, the method returns 0 (False).
96
95
97
-
```
98
-
DECLARE @x xml (Production.ManuInstructionsSchemaCollection);
96
+
```sql
97
+
DECLARE @x XML (Production.ManuInstructionsSchemaCollection);
99
98
SELECT @x=Instructions
100
99
FROMProduction.ProductModel
101
100
WHERE ProductModelID=67;
102
101
--SELECT @x
103
-
DECLARE @f int;
102
+
DECLARE @f INT;
104
103
SET @f = @x.exist(' declare namespace AWMI="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions";
105
104
/AWMI:root/AWMI:Location[@LocationID=50]
106
105
');
@@ -110,7 +109,7 @@ SELECT @f;
110
109
### Example: Specifying the exist() method against an xml type column
111
110
The following query retrieves product model IDs whose catalog descriptions do not include the specifications, <`Specifications`> element:
@@ -136,7 +135,7 @@ WHERE CatalogDescription.exist('
136
135
137
136
The query specifies **query()** and **exist()** methods of the xml data type and both these methods declare the same namespaces in the query prolog. In this case, you may want to use WITH XMLNAMESPACES to declare the prefix and use it in the query.
138
137
139
-
```
138
+
```sql
140
139
WITH XMLNAMESPACES ('https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription'AS pd)
0 commit comments