简体   繁体   English

使用新架构更新XML列架构

[英]Updating XML column schema with new schema

I am using SQL Server 2008 and busy doing a POC where I have a table with a typed XML column. 我正在使用SQL Server 2008,并且忙于做POC,其中有一个带有类型化XML列的表。 The nature of the POC is that the XML will change over time as our needs evolve for the data that gets stored in it. POC的性质是,随着我们对存储在其中的数据的需求不断发展,XML会随着时间而变化。 (We are serializing objects in C# to persist the data to the DB) (我们正在序列化C#中的对象,以将数据持久保存到DB中)

However, I need to keep the old XML in the table for auditing purposes and be able to validate any new XML that gets added to it correctly with the updated schema. 但是,出于审计目的,我需要将旧的XML保留在表中,并能够验证使用更新的架构正确添加到其中的任何新XML。

I have tried dropping the schema from the collection, updating it (in this case adding a required attribute), adding it back to the collection and then binding it to the column it belongs to. 我尝试过从集合中删除模式,对其进行更新(在这种情况下,添加必需的属性),将其添加回集合中,然后将其绑定到它所属的列。 When doing so I simply get a validation error that the XML already in the column fails the validation because it does not contain the new attribute. 这样做时,我只是得到一个验证错误,因为该列中已存在的XML不包含新属性,因此该验证失败。

Is there a way to update the XML column to use a new Schema and ignore the XML that is already stored in the column? 有没有一种方法可以更新XML列以使用新的架构,而忽略该列中已经存储的XML? Or would it be possible to disable validation while adding the new schema to the XML column? 还是在将新架构添加到XML列时禁用验证?

A schema collection should permit you to have different versions of the XML data without having to ignore validation. 模式集合应允许您具有不同版本的XML数据,而不必忽略验证。

Have a look at XML Schema Collections to start with. 看一看XML模式集合

In your T-SQL code, you'd have something like this (this code is sourced from Bob Beauchemin of SQLskills.com): 在您的T-SQL代码中,您将具有以下内容(此代码来自SQLskills.com的Bob Beauchemin ):

-- Load XML schema from file
DECLARE @x XML
SET @x = (
SELECT * FROM OPENROWSET(
   BULK 'C:\invoice.xsd',
           SINGLE_BLOB
   ) AS x
)

-- And use it to create an XML schema collection
CREATE XML SCHEMA COLLECTION InvoiceType AS @x

Now you create a table which maps the column to the schema collection: 现在,您创建一个将列映射到架构集合的表:

CREATE TABLE invoice_docs (
   invoiceid INTEGER PRIMARY KEY IDENTITY,
   invoice   XML(document InvoiceType)
)

Now when your schema changes, you modify the schema collection by adding in the new version of the schema: 现在,当您的模式更改时,您可以通过添加新版本的模式来修改模式集合:

DECLARE @x XML
SET @x = (
SELECT * FROM OPENROWSET(
   BULK 'C:\invoice_v2.xsd',
           SINGLE_BLOB
   ) AS x
)

-- And use it to create an XML schema collection
-- Allow V1 and V2 invoices
ALTER XML SCHEMA COLLECTION InvoiceType ADD @x

The old data already in the table is validated against the old schema, and any new data will validate against the old or new schema. 表中已经存在的旧数据将根据旧模式进行验证,所有新数据都将根据旧模式新模式进行验证。

If you want to validate only against the newer schema, you'll have to add an additional constraint to the column. 如果只想针对较新的架构进行验证,则必须向该列添加其他约束。

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

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