Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to assign an XML Schema doc to a SQL Server's table's column with an XML data type

I'm using SQL Server 2008 R2 and have a simple table with a column of data type XML. I have created an XML Schema document that correctly validates the XML data that will be stored. My questions are:

  1. How can I assign the schema to column such that it will validate the XML and reject the insert if it fails the schema?

  2. Is the schema required in order to add an XML Index to the column or can XML Indices be added without the schema doc?

like image 467
millie Avatar asked Sep 19 '11 13:09

millie


1 Answers

To create a schema collection, you can use the following:

CREATE XML SCHEMA COLLECTION MyXmlSchema AS
N'YourSchemaHere';

Then, apply it with:

CREATE TABLE MyTable
(
   MyTableId INT PRIMARY KEY
   , XmlData XML (MyXmlSchema)
)

Referenced from: http://technet.microsoft.com/en-us/library/ms176009.aspx

This example shows you do not need to have a schema on your XML for you to be able to index the column: http://msdn.microsoft.com/en-us/library/bb510443.aspx

like image 54
Adam Wenger Avatar answered Sep 27 '22 20:09

Adam Wenger