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
{{ message }}
Repository navigation
Commit 88b84f1
Browse filesBrowse the repository at this point in the historyBrowse files
Update data type conversion chart and text (#38339)
* Update data type conversion chart and text
* Apply batched suggestions from code review
Co-authored-by: learn-build-service-prod-01[bot] <274427437+learn-build-service-prod-01[bot]@users.noreply.github.com>
Co-authored-by: Randolph West MSFT <97149825+rwestMSFT@users.noreply.github.com>
---------
Co-authored-by: learn-build-service-prod-01[bot] <274427437+learn-build-service-prod-01[bot]@users.noreply.github.com>
Copy file name to clipboardExpand all lines: docs/relational-databases/xml/compare-typed-xml-to-untyped-xml.md
+41-27Lines changed: 41 additions & 27 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,10 +1,9 @@
1
1
---
2
-
title: "Compare Typed XML to Untyped XML"
2
+
title: Compare Typed XML to Untyped XML
3
3
description: Learn about the differences between typed and untyped XML.
4
4
author: rwestMSFT
5
5
ms.author: randolphwest
6
-
ms.reviewer: randolphwest
7
-
ms.date: 05/05/2022
6
+
ms.date: 10/13/2026
8
7
ms.service: sql
9
8
ms.subservice: xml
10
9
ms.topic: how-to
@@ -44,7 +43,9 @@ An XML schema provides the following:
44
43
45
44
-**Data type information.** Schemas provide information about the types of attributes and elements in the **xml** data type instance. The type information provides more precise operational semantics to the values contained in the instance than is possible with untyped **xml**. For example, decimal arithmetic operations can be performed on a decimal value, but not on a string value. Because of this, typed XML storage can be made significantly more compact than untyped XML.
46
45
47
-
## Choosing typed or untyped XML
46
+
<aid="choosing-typed-or-untyped-xml"></a>
47
+
48
+
## Choose typed or untyped XML
48
49
49
50
Use untyped **xml** data type in the following situations:
50
51
@@ -62,9 +63,11 @@ Use typed **xml** data type in the following situations:
62
63
63
64
Typed XML columns, parameters, and variables can store XML documents or content. However, you have to specify with a flag whether you're storing a document or content at the time of declaration. Additionally, you have to provide the collection of XML schemas. Specify DOCUMENT if each XML instance has exactly one top-level element. Otherwise, use CONTENT. The query compiler uses the DOCUMENT flag in type checks during query compilation to infer singleton top-level elements.
64
65
65
-
## Creating typed XML
66
+
<aid="creating-typed-xml"></a>
67
+
68
+
## Create typed XML
66
69
67
-
Before you can create typed **xml** variables, parameters, or columns, you must first register the XML schema collection by using [CREATE XML SCHEMA COLLECTION(Transact-SQL)](../../t-sql/statements/create-xml-schema-collection-transact-sql.md). You can then associate the XML schema collection with variables, parameters, or columns of the **xml** data type.
70
+
Before you can create typed **xml** variables, parameters, or columns, you must first register the XML schema collection by using [CREATE XML SCHEMA COLLECTION](../../t-sql/statements/create-xml-schema-collection-transact-sql.md). You can then associate the XML schema collection with variables, parameters, or columns of the **xml** data type.
68
71
69
72
In the following examples, a two-part naming convention is used for specifying the XML schema collection name. The first part is the schema name, and the second part is the XML schema collection name.
70
73
@@ -73,17 +76,19 @@ In the following examples, a two-part naming convention is used for specifying t
73
76
The following example creates an **xml** type variable and associates a schema collection with it. The schema collection specified in the example is already imported in the **AdventureWorks** database.
74
77
75
78
```sql
76
-
DECLARE @x xml (Production.ProductDescriptionSchemaCollection);
79
+
DECLARE @x AS XML(Production.ProductDescriptionSchemaCollection);
77
80
```
78
81
79
82
### Example: Specifying a Schema for an xml type column
80
83
81
84
The following example creates a table with an **xml** type column and specifies a schema for the column:
82
85
83
86
```sql
84
-
CREATETABLET1(
85
-
Col1 int,
86
-
Col2 xml (Production.ProductDescriptionSchemaCollection));
Note the following about the XML schema collection:
101
108
102
-
- An XML schema collection is available only in the database in which it was registered by using [Creating an XML Schema Collection](../../t-sql/statements/create-xml-schema-collection-transact-sql.md).
109
+
- An XML schema collection is available only in the database in which it was registered by using [CREATE XML SCHEMA COLLECTION](../../t-sql/statements/create-xml-schema-collection-transact-sql.md).
103
110
104
111
- If you cast from a string to a typed **xml** data type, the parsing also performs validation and typing, based on the XML schema namespaces in the collection specified.
105
112
106
-
- You can cast from a typed **xml** data type to an untyped **xml** data type, and vice versa.
113
+
- You can cast from a typed **xml** data type to an untyped **xml** data type, and vice versa. This conversion can also be implicit.
114
+
- Conversion between typed **xml** values can be implicit only when both values use the same XML schema collection and the same `DOCUMENT` or `CONTENT` facet. Otherwise, the conversion must be explicit. An implicit conversion between different XML schema collections causes error 527, and an implicit conversion between different facets causes error 526.
115
+
- Conversion between a CLR user-defined type and typed **xml** isn't allowed. Conversion between a CLR user-defined type and untyped **xml** is allowed.
107
116
108
-
For more information about other ways to generate XML in SQL Server, see [Create Instances of XML Data](../../relational-databases/xml/create-instances-of-xml-data.md). After XML is generated, it can be assigned either to an **xml** data type variable or stored in **xml** type columns for additional processing.
117
+
For more information about other ways to generate XML in SQL Server, see [Create instances of XML data](create-instances-of-xml-data.md). After XML is generated, you can assign it either to an **xml** data type variable or store it in **xml** type columns for additional processing.
109
118
110
-
In the data type hierarchy, the **xml** data type appears below **sql_variant** and user-defined types, but above any of the built-in types.
119
+
In the [data type precedence](../../t-sql/data-types/data-type-precedence-transact-sql.md) order, the **xml** data type appears below **json** and **sql_variant**, and above user-defined types and all other data types. For example, an expression that combines **xml** and a CLR user-defined type returns **xml**.
111
120
112
121
### Example: Specifying facets to constrain a typed XML column
113
122
114
123
For typed **xml** columns, you can constrain the column to allow only single, top-level elements for each instance stored in it. You do this by specifying the optional `DOCUMENT` facet when a table is created, as shown in the following example:
You can specify the optional DOCUMENT/CONTENT facets anywhere you define **xml** type (typed xml). For example, when you create a typed **xml** variable, you can add the DOCUMENT/CONTENT facet, as shown in the following:
138
150
139
151
```sql
140
-
declare @x xml (DOCUMENT Production.ProductDescriptionSchemaCollection);
152
+
DECLARE @x AS XML(DOCUMENT Production.ProductDescriptionSchemaCollection);
141
153
```
142
154
143
155
## Document Type Definition (DTD)
@@ -146,21 +158,23 @@ The **xml** data type columns, variables, and parameters can be typed by using X
146
158
147
159
You can convert DTDs to XML schema documents by using third-party tools, and load the XML schemas into the database.
[!INCLUDE[sql2008-md](../../includes/sql2008-md.md)] made several extensions to the XML Schema support, including support for lax validation, improved handling of **xs:date**, **xs:time** and **xs:dateTime** instance data, and added support for list and union types. In most cases the changes don't affect the upgrade experience. However if you used an XML Schema collection in [!INCLUDE[ssVersion2005](../../includes/ssversion2005-md.md)] that allowed values of type **xs:date**, **xs:time**, or **xs:dateTime** (or any subtype) then the following upgrade steps occur when you attach your [!INCLUDE[ssVersion2005](../../includes/ssversion2005-md.md)] database to a later version of [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)]:
165
+
[!INCLUDE[sql2008-md](../../includes/sql2008-md.md)] made several extensions to the XML Schema support, including support for lax validation, improved handling of `xs:date`, `xs:time` and `xs:dateTime` instance data, and added support for list and union types. In most cases the changes don't affect the upgrade experience. However if you used an XML Schema collection in [!INCLUDE[ssVersion2005](../../includes/ssversion2005-md.md)] that allowed values of type `xs:date`, `xs:time`, or `xs:dateTime` (or any subtype) then the following upgrade steps occur when you attach your [!INCLUDE[ssVersion2005](../../includes/ssversion2005-md.md)] database to a later version of [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)]:
152
166
153
-
1. For every XML column, that is typed with an XML Schema Collection that contains elements or attributes that are typed as either **xs:anyType**, **xs:anySimpleType**, **xs:date** or any of its subtypes, **xs:time** or any subtype thereof, or **xs:dateTime** or any of its subtypes, or are union or list types containing any of these types the following occurs:
167
+
1. For every XML column, that is typed with an XML Schema Collection that contains elements or attributes that are typed as either `xs:anyType`, `xs:anySimpleType`, `xs:date` or any of its subtypes, `xs:time` or any subtype thereof, or `xs:dateTime` or any of its subtypes, or are union or list types containing any of these types the following occurs:
154
168
155
-
1. All XML indices on the column will be disabled.
169
+
1. All XML indices on the column are disabled.
156
170
157
-
2. All [!INCLUDE[ssVersion2005](../../includes/ssversion2005-md.md)] values will continue to be represented in the Z timezone, because they've been normalized to the Z timezone.
171
+
1. All [!INCLUDE[ssVersion2005](../../includes/ssversion2005-md.md)] values continue to be represented in the `Z` timezone, because they're normalized to the `Z` timezone.
158
172
159
-
3. Any **xs:date** or **xs:dateTime** values that are smaller than January 1 of year 1 will lead to a runtime error when the index gets rebuild or an XQuery or XML-DML statements gets executed against the **xml** data type containing that value.
173
+
1. Any `xs:date` or `xs:dateTime` values that are smaller than January 1 of year 1 cause a runtime error when the index is rebuilt, or when XQuery or XML-DML statements are executed against the **xml** data type containing that value.
160
174
161
-
2. Any negative years in **xs:date** or **xs:dateTime** facets or default values in an XML Schema collection will automatically be updated to the smallest value allowed by the base **xs:date** or **xs:dateTime** type (for example, 0001-01-01T00:00:00.0000000Z for **xs:dateTime**).
175
+
1. Any negative years in `xs:date` or `xs:dateTime` facets or default values in an XML Schema collection automatically update to the smallest value allowed by the base `xs:date` or `xs:dateTime` type (for example, 0001-01-01T00:00:00.0000000Z for `xs:dateTime`).
162
176
163
-
You can still use a Transact-SQL SELECT statement to retrieve the whole **xml** data type, even if it contains negative years. It's recommended that you replace negative years with a year within the newly supported range or change the type of the element or attribute to **xs:string**.
177
+
You can still use a Transact-SQL SELECT statement to retrieve the whole **xml** data type, even if it contains negative years. Replace negative years with a year within the newly supported range or change the type of the element or attribute to `xs:string`.
0 commit comments