Skip to content

Commit 5fada96

Browse files
authored
Update exist-method-xml-data-type.md
1 parent 5959a4d commit 5fada96

1 file changed

Lines changed: 19 additions & 20 deletions

File tree

docs/t-sql/xml/exist-method-xml-data-type.md

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ ms.author: genemi
2929

3030
## Syntax
3131

32-
```
33-
32+
```syntaxsql
3433
exist (XQuery)
3534
```
3635

@@ -45,10 +44,10 @@ exist (XQuery)
4544
> [!NOTE]
4645
> 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:
4746
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()');
5251
```
5352

5453
## Examples
@@ -57,12 +56,12 @@ select @x.exist('true()');
5756
### Example: Specifying the exist() method against an xml type variable
5857
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`.
5958

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;
6665
```
6766

6867
In comparing dates in the **exist()** method, note the following:
@@ -75,9 +74,9 @@ select @f;
7574

7675
The following example is similar to the previous one, except it has a <`Somedate`> element.
7776

78-
```
79-
DECLARE @x xml;
80-
DECLARE @f bit;
77+
```sql
78+
DECLARE @x XML;
79+
DECLARE @f BIT;
8180
SET @x = '<Somedate>2002-01-01Z</Somedate>';
8281
SET @f = @x.exist('/Somedate[(text()[1] cast as xs:date ?) = xs:date("2002-01-01Z") ]')
8382
SELECT @f;
@@ -94,13 +93,13 @@ SELECT @f;
9493

9594
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).
9695

97-
```
98-
DECLARE @x xml (Production.ManuInstructionsSchemaCollection);
96+
```sql
97+
DECLARE @x XML (Production.ManuInstructionsSchemaCollection);
9998
SELECT @x=Instructions
10099
FROM Production.ProductModel
101100
WHERE ProductModelID=67;
102101
--SELECT @x
103-
DECLARE @f int;
102+
DECLARE @f INT;
104103
SET @f = @x.exist(' declare namespace AWMI="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions";
105104
/AWMI:root/AWMI:Location[@LocationID=50]
106105
');
@@ -110,7 +109,7 @@ SELECT @f;
110109
### Example: Specifying the exist() method against an xml type column
111110
The following query retrieves product model IDs whose catalog descriptions do not include the specifications, <`Specifications`> element:
112111

113-
```
112+
```sql
114113
SELECT ProductModelID, CatalogDescription.query('
115114
declare namespace pd="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription";
116115
<Product
@@ -136,7 +135,7 @@ WHERE CatalogDescription.exist('
136135

137136
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.
138137

139-
```
138+
```sql
140139
WITH XMLNAMESPACES ('https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription' AS pd)
141140
SELECT ProductModelID, CatalogDescription.query('
142141
<Product

0 commit comments

Comments
 (0)