Sql 比较更新脚本的xml架构列

Sql 比较更新脚本的xml架构列,sql,tsql,Sql,Tsql,有没有办法比较两个xml列模式,以便触发该列的模式更新 我知道如何查询存在的模式,但是否有其他方法可以将其与可能更新的模式进行比较 select C2.name from sys.columns as C1 inner join sys.xml_schema_collections as C2 on C1.xml_collection_id = C2.xml_collection_id where C1.object_id = object_id('TableName') and

有没有办法比较两个xml列模式,以便触发该列的模式更新

我知道如何查询存在的模式,但是否有其他方法可以将其与可能更新的模式进行比较

select C2.name
from sys.columns as C1
  inner join sys.xml_schema_collections as C2
    on C1.xml_collection_id = C2.xml_collection_id
where C1.object_id = object_id('TableName') and
      C1.name = 'ColumnName' and C2.name = 'SchemaName'
就像这样:

IF NOT EXISTS (< Actual schema compare >)
BEGIN
    --ALTER COLUMN to xml
    --DROP SCHEMA
    --CREATE SCHEMA
    --ALTER COLUMN to xml with schema again
END
如果不存在()
开始
--将列更改为xml
--删除模式
--创建模式
--再次使用模式将列更改为xml
结束
将以XML形式提供集合中的模式

存储在SQL Server中的XML与用于创建架构的版本不完全相同。注释、空白和注释被删除,因此您必须在SQL Server中为要比较的架构创建临时集合,并使用
xml\u schema\u namespace
提取该架构。XML不能直接比较,因此在比较模式集合之前,必须将XML转换为
nvarchar(max)

检查模式排序规则的存储过程可能如下所示

create procedure dbo.CompareSchema 
  @NewSchema xml,
  @RelationalSchema sysname,
  @XMLSchemaCollectionName sysname,
  @Diff bit out
as
begin
  create xml schema collection dbo.__TempCollaction as @NewSchema;

  if cast(xml_schema_namespace(@RelationalSchema, @XMLSchemaCollectionName) as nvarchar(max)) = 
       cast(xml_schema_namespace('dbo', '__TempCollaction') as nvarchar(max))
    set @Diff = 0;
  else
    set @Diff = 1;   

  drop xml schema collection dbo.__TempCollaction;
end

谢谢@Mikael Eriksson,我明天试试这个。