Skip to content

Commit ea45735

Browse files
committed
Fix value method example and clarify type (UUF 236969 and 253168)
1 parent a680ef8 commit ea45735

1 file changed

Lines changed: 42 additions & 6 deletions

File tree

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

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ The *XQuery* expression, a string literal, that retrieves data inside the XML in
3838

3939
#### *SQLType*
4040

41-
The preferred SQL type, a string literal, to be returned. The return type of this method matches the *SQLType* parameter. *SQLType* can't be an **xml** data type, a common language runtime (CLR) user-defined type, **image**, **text**, **ntext**, or **sql_variant** data type. *SQLType* can be a user-defined SQL data type.
41+
The preferred SQL type, a string literal, to be returned. The return type of this method matches the *SQLType* parameter. *SQLType* can be a user-defined SQL data type.
42+
43+
> [!NOTE]
44+
> *SQLType* can't be one of the following data types: **xml**, **image**, **text**, **ntext**, **sql_variant**, or a common language runtime (CLR) user-defined type.
4245
4346
The `value()` method uses the [!INCLUDE [tsql](../../includes/tsql-md.md)] `CONVERT` operator implicitly. `value()` tries to convert the result of the XQuery expression, the serialized string representation, from XML Schema Definition (XSD) type to the corresponding SQL type specified by [!INCLUDE [tsql](../../includes/tsql-md.md)] conversion. For more information about type casting rules for `CONVERT`, see [CAST and CONVERT](../functions/cast-and-convert-transact-sql.md).
4447

@@ -73,7 +76,7 @@ A value of `1` is returned as a result.
7376

7477
Although there's only one `ProductID` attribute in the XML instance, the static typing rules require you to explicitly specify that the path expression returns a singleton. Therefore, the `[1]` is added to the end of the path expression. For more information about static typing, see [XQuery and Static Typing](../../xquery/xquery-and-static-typing.md).
7578

76-
### B. Use the value() method to retrieve a value from an XML type column
79+
### B. Use the value() method to retrieve an integer value from an XML type column
7780

7881
The following query is specified against an **xml** type column (`CatalogDescription`) in the [!INCLUDE [sssampledbobject-md](../../includes/sssampledbobject-md.md)] database. The query retrieves `ProductModelID` attribute values from each XML instance stored in the column.
7982

@@ -92,15 +95,48 @@ Note from the previous query:
9295

9396
- Per static typing requirements, `[1]` is added at the end of the path expression in the `value()` method to explicitly indicate that the path expression returns a singleton.
9497

95-
Here's the partial result:
98+
[!INCLUDE [ssresult-md](../../includes/ssresult-md.md)]
9699

97100
```output
98101
35
99102
34
100-
...
103+
28
104+
25
105+
23
106+
19
107+
```
108+
109+
### C. Use the value() method to retrieve a string value from an XML type column
110+
111+
The following query is specified against the **xml** type column (`CatalogDescription`) in the [!INCLUDE [sssampledbobject-md](../../includes/sssampledbobject-md.md)] database. The query retrieves `ProductModelName` attribute values from each XML instance stored in the column.
112+
113+
```sql
114+
SELECT CatalogDescription.value(
115+
'declare namespace PD="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription";
116+
(/PD:ProductDescription/@ProductModelName)[1]', 'varchar(50)') AS Result
117+
FROM Production.ProductModel
118+
WHERE CatalogDescription IS NOT NULL
119+
ORDER BY Result DESC;
120+
```
121+
122+
Note from the previous query:
123+
124+
- The `namespace` keyword is used to define a namespace prefix.
125+
126+
- Per static typing requirements, `[1]` is added at the end of the path expression in the `value()` method to explicitly indicate that the path expression returns a singleton.
127+
128+
[!INCLUDE [ssresult-md](../../includes/ssresult-md.md)]
129+
130+
```output
131+
Touring-2000
132+
Touring-1000
133+
Road-450
134+
Road-150
135+
Mountain-500
136+
Mountain 100
101137
```
102138

103-
### C. Use the value() and exist() methods to retrieve values from an XML type column
139+
### D. Use the value() and exist() methods to retrieve values from an XML type column
104140

105141
The following example shows using both the `value()` method and the [exist() method](exist-method-xml-data-type.md) of the **xml** data type. The `value()` method is used to retrieve `ProductModelID` attribute values from the XML. The `exist()` method in the `WHERE` clause is used to filter the rows from the table.
106142

@@ -134,7 +170,7 @@ Here's the partial result:
134170
...
135171
```
136172

137-
### D. Use the exist() method instead of the value() method
173+
### E. Use the exist() method instead of the value() method
138174

139175
For performance reasons, instead of using the `value()` method in a predicate to compare with a relational value, use `exist()` with `sql:column()`. For example:
140176

0 commit comments

Comments
 (0)