Change existing columns to XML columns

Applies to: yesSQL Server (all supported versions) YesAzure SQL Database

The ALTER TABLE statement supports the xml data type. For example, you can alter any string type column to the xml data type. In these cases, the documents contained in the column must be well formed. Also, if you're changing the type of the column from string to typed xml, the documents in the column are validated against the specified XSD schemas.

CREATE TABLE T (Col1 int primary key, Col2 nvarchar(max));
GO
INSERT INTO T
  VALUES (1, '<Root><Product ProductID="1"/></Root>');
GO
ALTER TABLE T
  ALTER COLUMN Col2 xml;

You can change an xml type column from untyped XML to typed XML. For example:

CREATE TABLE T (Col1 int primary key, Col2 xml);
GO
INSERT INTO T
  values (1, '<p1:ProductDescription ProductModelID="1"
xmlns:p1="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription">
            </p1:ProductDescription>');
GO
-- Make it a typed xml column by specifying a schema collection.
ALTER TABLE T
  ALTER COLUMN Col2 xml (Production.ProductDescriptionSchemaCollection);

Note

The script will run against AdventureWorks2012 database, because the XML schema collection, Production.ProductDescriptionSchemaCollection, is created as part of the AdventureWorks2012 database.

In the previous example, all the instances stored in the column are validated and typed against the XSD schemas in the specified collection. If the column contains one or more XML instances that are invalid regarding the specified schema, the ALTER TABLE statement will fail and you won't be able to change your untyped XML column into typed XML.

Note

If a table is large, modifying an xml type column can be costly. This is because each document must be checked for being well formed and, for typed XML, must also be validated.

See also