我正在使用SELECT
语句创建一个xml文件,我在向文档添加额外节点时遇到了问题。
创建的当前结构是:
<DBObjectsToUpdate>
<TablesToAdd>...</TablesToAdd>
<TableObjectsToDelete> ...</TableObjectsToDelete>
<Views>
<View> </View>
</Views>
<StoredProcedures>
<StoredProc> </StoredProc>
</StoredProcedures>
</DBObjectsToUpdate>
我想要做的是在“视图”周围添加一个周围的节点。和&#39; StoredProcedures&#39;。所以它看起来像这样:
<DBObjectsToUpdate>
<TablesToAdd>...</TablesToAdd>
<TableObjectsToDelete> ...</TableObjectsToDelete>
<TableObjectsToAdd>
<Views>
<View> </View>
</Views>
<StoredProcedures>
<StoredProc> </StoredProc>
</StoredProcedures>
</TableObjectsToAdd>
</DBObjectsToUpdate>
这是select语句的相关部分:
SELECT
[Table].TableName AS "@TName", SchemaName AS "@Schema",
TextImageFileGroup AS "@TextImageOnFileGroup",
..bunch of stuff that is not important for this question
FROM
( a select statement to get the tables) AS [Table]
(--Returns all of the Triggers - Currently there are no triggers
SELECT
[Trig].TriggerName AS "@TriggerName",
[Trig].TrigDefinition AS "@TrigDefinition"
FROM
(SELECT DISTINCT S7.TableName, S7.TriggerName, TrigDefinition
FROM #SourceDBObjects S7
JOIN #TableObjectsToAdd T7 ON S7.TableName = T7.TableName
AND S7.TriggerName = T7.TriggerName
AND T7.TriggerName IS NOT NULL) AS [Trig]
WHERE
[Table].TableName = [Trig].TableName
FOR XML PATH('Trigger'), TYPE) AS [Triggers]
FROM
(SELECT DISTINCT T.TableName, S.SchemaName, S.TextImageFileGroup
FROM #TableObjectsToAdd T
JOIN #SourceDBObjects S ON T.TableName = S.TableName) AS [Table]
ORDER BY
"@TName"
FOR XML PATH ('Table'), TYPE) as [TableObjectsToAdd],
(--Returns all of the check constrains for table
SELECT [CkCon].CheckName AS "@CkName", [CkCon].CkDefinition AS "@CkDefinition", [CkCon].IsCkDisabled AS "@IsCkDisabled"
FROM (SELECT DISTINCT T5.TableName, T5.CheckName, CkDefinition, IsCkDisabled
FROM #TargetDBObjects T5 JOIN #TableObjectsToDelete D5 ON T5.TableName = D5.TableName AND T5.DefaultName = T5.CheckName AND D5.CheckName IS NOT NULL) AS [CkCon]
WHERE [Table].TableName = [CkCon].TableName
FOR XML PATH('Check'),
TYPE
) AS [CheckConstraints]
FROM (
SELECT DISTINCT D.TableName, T.SchemaName, T.TextImageFileGroup
FROM #TableObjectsToDelete D JOIN #TargetDBObjects T ON T.TableName = D.TableName
) AS [Table]
ORDER BY "@TName"
FOR XML PATH ('Table'), TYPE)
as [TableObjectsToDelete],
( ------VIEWS NODE------
SELECT ObjectName AS "@VName", REPLACE(ObjectText, '''', '''''') AS "@VDefinition"
FROM #SourceViewsSPFunctionsToAdd
WHERE ObjectType = 'View'
FOR XML PATH ('View'), TYPE
) AS [Views],
( ------STORED PROCEDURES NODE------
SELECT ObjectName AS "@SPName", REPLACE(ObjectText, '''', '''''') AS "@SPDefinition"
FROM #SourceViewsSPFunctionsToAdd
WHERE ObjectType = 'StoredProcedure'
FOR XML PATH ('StoredProcedure'), TYPE
) AS [StoredProcedures],
FOR XML PATH(''),
ROOT('DBObjectsToUpdate')
如何将视图和存储过程节点封装到没有元素或属性的外部节点?
答案 0 :(得分:1)
我想出来了......使用SELECT FOR XML PATH(''), TYPE) AS [NameOfNode]
所以有问题的select语句部分如下所示: ...
(SELECT
( ------VIEWS NODE------
SELECT ObjectName AS "@VName", REPLACE(ObjectText, '''', '''''') AS "@VDefinition"
FROM #SourceViewsSPFunctionsToAdd
WHERE ObjectType = 'View'
FOR XML PATH ('View'), TYPE
) AS [Views],
( ------STORED PROCEDURES NODE------
SELECT ObjectName AS "@SPName", REPLACE(ObjectText, '''', '''''') AS "@SPDefinition"
FROM #SourceViewsSPFunctionsToAdd
WHERE ObjectType = 'StoredProcedure'
FOR XML PATH ('StoredProcedure'), TYPE
) AS [StoredProcedures],
FOR XML PATH (''), TYPE) as [DBObjectsToAdd]