修改SQL Server架构集合

mev*_*ven 14 xml database sql-server xsd xml-schema-collection

SQL Server XML Schema Collection是一个有趣的概念,我发现它在设计动态数据内容时非常有用.但是,当我通过实现Schema Collections时,我发现维护它们非常困难.

模式集合DDL仅允许CREATE和ALTER/ADD节点到现有方案.

CREATE XML SCHEMA COLLECTION [ <relational_schema>. ]sql_identifier AS 'XSD Content'
ALTER XML SCHEMA COLLECTION [ <relational_schema>. ]sql_identifier ADD 'Schema Component'
Run Code Online (Sandbox Code Playgroud)

如果要从架构中删除任何节点,则必须发出以下DDL.

  1. 如果将该架构集合分配给表列,则必须更改表以从该列中删除架构集合关联
  2. 删除架构集合对象
  3. 重新创建架构集合
  4. 更改表列以将架构集合重新关联到该列.

当谈到集合中的100多个方案时,这是痛苦的.此外,您还必须重新创建XML索引(如果有的话).

任何解决方案,建议和技巧使这个架构集合对象编辑过程更容易?

Lar*_*mie 3

我同意 David 的观点,即 XML 并不是我们被告知的万能药,但在某些情况下它要么是不可避免的,要么是完成工作的最佳工具。不过,架构维护很痛苦。我只有几个要处理,但仍然浪费了几个小时。

这个脚本可能会有所帮助。它会生成您需要的表删除和添加。需要对其进行修改以包含 UDF 或可能引用 XML 模式的其他对象。要生成添加架构语句,我建议您使用 Mgt Studio 任务菜单中的“生成脚本...”功能,并将其保存到脚本的第 2 步。

SET NOCOUNT ON

/* 
    1) Save cols to table var
*/
DECLARE @xmlCols TABLE (
numID INTEGER IDENTITY(1,1),
TBL nvarchar(1024),
COL nvarchar(1024),
SCH nvarchar(1024)
);

insert into @xmlCols (TBL,COL,SCH)
SELECT DISTINCT OBJECT_NAME(colm.object_id) AS 'TABLE', colm.name AS 'COLUMN', coll.name AS 'Schema' 
FROM  sys.columns colm
   inner JOIN     sys.xml_schema_collections coll
        ON colm.xml_collection_id = coll.xml_collection_id
ORDER BY OBJECT_NAME(colm.object_id), colm.name   

DECLARE @lastRow as int
DECLARE @currentRow as int
DECLARE @dbName as varchar(1024)
DECLARE @tableName as varchar(1024)
DECLARE @colName as varchar(1024)
DECLARE @schemaName as varchar(1024)
SET @lastRow = @@ROWCOUNT
SET @currentRow = @lastRow
SET @dbName = 'dbNAme'

print ''
print '--!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'
print '--!!!!! Scipt Schemas and Save in Mgt Studio !!!!'
print '--!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'
print ''


print ''
print '--!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'
print '--!!!!! Omit Schemas from COls !!!!'
print '--!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'
print ''
--omit the Schema for each column
WHILE @currentRow <> 0
BEGIN
    SELECT @tableName=TBL, @colName=COL, @schemaName=SCH from @xmlCols WHERE numID = @currentRow

    print N'ALTER TABLE [' + @tableName + N'] ALTER COLUMN ['+ @colName + N'] XML'

    set @currentRow = @currentRow -1
END

print ''
print '--!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'
print '--!!!!! drop your xml schema(s)  !!!!'
print '--!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'
print ''
SET @currentRow = @lastRow
WHILE @currentRow <> 0
BEGIN
    SELECT @tableName=TBL, @colName=COL, @schemaName=SCH from @xmlCols WHERE numID = @currentRow

    print N'DROP XML SCHEMA COLLECTION [dbo].['+@schemaName+']'

    set @currentRow = @currentRow -1
END

print ''
print '--!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'
print '--!!!!! CLean your Tables      !!!!'
print '--!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'
print ''

--clean up the tables
SET @currentRow = @lastRow
WHILE @currentRow <> 0
BEGIN
    SELECT @tableName=TBL, @colName=COL, @schemaName=SCH from @xmlCols WHERE numID = @currentRow

    print N'DBCC CleanTable (''' + @dbName + N''', ''' + @tableName + N''', 0)'

    set @currentRow = @currentRow -1
END

print ''
print '--!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'
print '--!!!!! Run XML Schema Scripts !!!!'
print '--!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'
print ''
SET @currentRow = @lastRow
WHILE @currentRow <> 0
BEGIN
    SELECT @tableName=TBL, @colName=COL, @schemaName=SCH from @xmlCols WHERE numID = @currentRow

    print N'ALTER TABLE [' + @tableName + N'] ALTER COLUMN ['+ @colName + N'] XML('+ @schemaName + N')'''

    set @currentRow = @currentRow -1
END
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你。