简体   繁体   English

如何将XML架构文档分配给具有XML数据类型的SQL Server表的列

[英]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 . 我正在使用SQL Server 2008 R2,并且有一个包含数据类型XML列的简单表。 I have created an XML Schema document that correctly validates the XML data that will be stored. 我创建了一个XML Schema文档,可以正确验证将要存储的XML数据。 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? 如何将模式分配给列,以便在模式失败时验证XML并拒绝插入?

  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? 是否需要架构才能将XML索引添加到列中,或者可以在没有架构文档的情况下添加XML索引?

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 参考自: 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 此示例显示您无需在XML上具有架构,以便能够为列编制索引: http//msdn.microsoft.com/en-us/library/bb510443.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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