Skip to content

Commit 88b84f1

Browse 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>
1 parent fa6f181 commit 88b84f1

18 files changed

Lines changed: 743 additions & 596 deletions

‎.openpublishing.redirection.json‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68461,7 +68461,12 @@
6846168461
},
6846268462
{
6846368463
"source_path": "docs/t-sql/spatial-geography/index.md",
68464-
"redirect_url": "/sql/t-sql/spatial-geography/spatial-types-geography",
68464+
"redirect_url": "/sql/t-sql/spatial-geography/spatial-types-geography-transact-sql",
68465+
"redirect_document_id": false
68466+
},
68467+
{
68468+
"source_path": "docs/t-sql/spatial-geography/spatial-types-geography.md",
68469+
"redirect_url": "/sql/t-sql/spatial-geography/spatial-types-geography-transact-sql",
6846568470
"redirect_document_id": false
6846668471
},
6846768472
{

‎docs/relational-databases/xml/compare-typed-xml-to-untyped-xml.md‎

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
---
2-
title: "Compare Typed XML to Untyped XML"
2+
title: Compare Typed XML to Untyped XML
33
description: Learn about the differences between typed and untyped XML.
44
author: rwestMSFT
55
ms.author: randolphwest
6-
ms.reviewer: randolphwest
7-
ms.date: 05/05/2022
6+
ms.date: 10/13/2026
87
ms.service: sql
98
ms.subservice: xml
109
ms.topic: how-to
@@ -44,7 +43,9 @@ An XML schema provides the following:
4443

4544
- **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.
4645

47-
## Choosing typed or untyped XML
46+
<a id="choosing-typed-or-untyped-xml"></a>
47+
48+
## Choose typed or untyped XML
4849

4950
Use untyped **xml** data type in the following situations:
5051

@@ -62,9 +63,11 @@ Use typed **xml** data type in the following situations:
6263

6364
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.
6465

65-
## Creating typed XML
66+
<a id="creating-typed-xml"></a>
67+
68+
## Create typed XML
6669

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 &#40;Transact-SQL&#41;](../../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.
6871

6972
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.
7073

@@ -73,17 +76,19 @@ In the following examples, a two-part naming convention is used for specifying t
7376
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.
7477

7578
```sql
76-
DECLARE @x xml (Production.ProductDescriptionSchemaCollection);
79+
DECLARE @x AS XML(Production.ProductDescriptionSchemaCollection);
7780
```
7881

7982
### Example: Specifying a Schema for an xml type column
8083

8184
The following example creates a table with an **xml** type column and specifies a schema for the column:
8285

8386
```sql
84-
CREATE TABLE T1(
85-
Col1 int,
86-
Col2 xml (Production.ProductDescriptionSchemaCollection));
87+
CREATE TABLE T1
88+
(
89+
Col1 INT,
90+
Col2 XML(Production.ProductDescriptionSchemaCollection)
91+
);
8792
```
8893

8994
### Example: Passing an XML type parameter to a stored procedure
@@ -92,31 +97,38 @@ The following example passes an **xml** type parameter to a stored procedure and
9297

9398
```sql
9499
CREATE PROCEDURE SampleProc
95-
@ProdDescription xml (Production.ProductDescriptionSchemaCollection)
100+
(
101+
@ProdDescription XML(Production.ProductDescriptionSchemaCollection)
102+
)
96103
AS
97104
...
98105
```
99106

100107
Note the following about the XML schema collection:
101108

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).
103110

104111
- 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.
105112

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.
107116

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.
109118

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**.
111120

112121
### Example: Specifying facets to constrain a typed XML column
113122

114123
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:
115124

116125
```sql
117-
CREATE TABLE T(Col1 xml
118-
(DOCUMENT Production.ProductDescriptionSchemaCollection));
126+
CREATE TABLE T
127+
(
128+
Col1 XML(DOCUMENT Production.ProductDescriptionSchemaCollection)
129+
);
119130
GO
131+
120132
DROP TABLE T;
121133
GO
122134
```
@@ -130,14 +142,14 @@ By default, instances stored in the typed **xml** column are stored as XML conte
130142
You can also explicitly specify this behavior by adding `CONTENT` facet, as shown in the following example:
131143

132144
```sql
133-
CREATE TABLE T(Col1 xml(CONTENT Production.ProductDescriptionSchemaCollection));
145+
CREATE TABLE T(Col1 XML(CONTENT Production.ProductDescriptionSchemaCollection));
134146
GO -- Default
135147
```
136148

137149
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:
138150

139151
```sql
140-
declare @x xml (DOCUMENT Production.ProductDescriptionSchemaCollection);
152+
DECLARE @x AS XML(DOCUMENT Production.ProductDescriptionSchemaCollection);
141153
```
142154

143155
## Document Type Definition (DTD)
@@ -146,21 +158,23 @@ The **xml** data type columns, variables, and parameters can be typed by using X
146158

147159
You can convert DTDs to XML schema documents by using third-party tools, and load the XML schemas into the database.
148160

149-
## Upgrading typed XML from SQL Server 2005
161+
<a id="upgrading-typed-xml-from-sql-server-2005"></a>
162+
163+
## Upgrade typed XML from SQL Server 2005
150164

151-
[!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)]:
152166

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:
154168

155-
1. All XML indices on the column will be disabled.
169+
1. All XML indices on the column are disabled.
156170

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.
158172

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.
160174

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`).
162176

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`.
164178

165179
## Related content
166180

0 commit comments

Comments
 (0)