简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM