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'
如果要从架构中删除任何节点,则必须在DDL之后发出。
对于集合中的100多个方案而言,这是一种痛苦。此外,您还必须重新创建XML索引(如果有的话)。
使这个架构集合对象编辑过程更容易的任何解决方案,建议和技巧?
答案 0 :(得分:3)
我同意大卫的观点,即XML不是我们被告知的灵丹妙药,但在某些情况下,它不可避免或是工作的最佳工具。但是,架构维护很痛苦。我只有几个人要处理,但仍然会浪费时间。
此脚本可能有所帮助。它会生成表格下降并添加您需要的内容。需要进行修改以包含可能引用XML模式的UDF或其他对象。要生成添加模式语句,我建议您使用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
希望它有所帮助。