由此,我使用递归函数获得分层XML,并且我已经做到了这一点:
<AssignTrees>
<AssignTree ID="1" ParentID="0" TreeDesc="SD1" IsSelected="0"> (this should be NULL)
<ChildAssigns>
<AssignTree ID="11" ParentID="1" TreeDesc="SD6" IsSelected="0"> (this should be 1)
<ChildAssigns>
<AssignTree ID="111" ParentID="11" TreeDesc="SD8" IsSelected="1">
<AssignDetail Assign="Rule6" Desc="Desc6" />
</AssignTree>
<AssignTree ID="112" ParentID="11" TreeDesc="SD15" IsSelected="1">
<AssignDetail Assign="Rule5" Desc="Desc5" />
</AssignTree>
</ChildAssigns>
</AssignTree>
<AssignTree ID="12" ParentID="1" TreeDesc="SD7" IsSelected="0"> (this should be 1)
<ChildAssigns>
<AssignTree ID="121" ParentID="12" TreeDesc="SD9" IsSelected="1">
<AssignDetail Assign="Rule2" Desc="Desc2" />
</AssignTree>
<AssignTree ID="122" ParentID="12" TreeDesc="SD10" IsSelected="1">
<AssignDetail Assign="Rule3" Desc="Desc3" />
</AssignTree>
</ChildAssigns>
</AssignTree>
<AssignTree ID="13" ParentID="1" TreeDesc="SD16" IsSelected="0">
<ChildAssigns>
<AssignTree ID="131" ParentID="13" TreeDesc="SD17" IsSelected="0">
<AssignDetail Assign="Rule7" Desc="Desc7" />
</AssignTree>
<AssignTree ID="132" ParentID="13" TreeDesc="SD18" IsSelected="0">
<AssignDetail Assign="Rule8" Desc="Desc8" />
</AssignTree>
</ChildAssigns>
</AssignTree>
</ChildAssigns>
</AssignTree>
<AssignTree ID="2" ParentID="0" TreeDesc="SD2" IsSelected="0"> (this should be NULL)
<ChildAssigns>
<AssignTree ID="21" ParentID="2" TreeDesc="SD11" IsSelected="0"> (this should be 1)
<ChildAssigns>
<AssignTree ID="211" ParentID="21" TreeDesc="SD13" IsSelected="1">
<AssignDetail Assign="Rule4" Desc="Desc4" />
</AssignTree>
<AssignTree ID="212" ParentID="21" TreeDesc="SD14" IsSelected="1">
<AssignDetail Assign="Rule10" Desc="Desc10" />
</AssignTree>
</ChildAssigns>
</AssignTree>
<AssignTree ID="22" ParentID="2" TreeDesc="SD12" IsSelected="0">
<AssignDetail Assign="Rule1" Desc="Desc1" />
</AssignTree>
</ChildAssigns>
</AssignTree>
</AssignTrees>
&#13;
&#39; IsSelected&#39;你在结构中看到的列实际上并不是表格本身的一部分,我用它来检查一个&#39;分配&#39;被分配到&#39; OBJ1&#39;。在XML中,父母&#39; AssignTree&#39;没有&#39;规则&#39;与它直接相关。因此,如果所有孩子都分配了这些孩子。那个&#39; ParentID&#39;被分配到&#;; OBJ1&#39;其被选中&#39;被选中&#39;属性应该是1.如果没有孩子被分配到&#;; OBJ1&#39;那么&#39; IsSelected&#39; = 0并且如果只分配了一些,则选择&#39; IsSelected&#39; = NULL。我对如何实现这一点感到茫然。
任何指针都会有所帮助。提前致谢。如果问题需要更多信息,请告诉我。
我的查询:
SELECT @o_ResultXml = ISNULL(CONVERT(VARCHAR(MAX),
(SELECT ATree.ID AS ID,
ATree.ParentID AS ParentID,
ATree.Assign AS Assign,
ATree.TreeDesc AS [Desc],
CASE
WHEN EXISTS ( SELECT 1
FROM ObjAssign obj WITH(NOLOCK)
WHERE obj.[Object] = 'OBJ1'
AND obj.Assign = ATree.Assign)
THEN @TRUE
ELSE @FALSE
END AS IsSelected,
(SELECT t.Assign AS Assign,
t.[Desc] AS [Desc]
FROM AssignDetails t WITH(NOLOCK)
WHERE t.Assign = ATree.Assign
FOR XML RAW('AssignDetail'), TYPE),
dbo.udf_SelectChildAssigns(ATree.ID, 'OBJ1')
AS ChildAssigns
FROM AssignTree ATree
WHERE ATree.ParentID IS NULL
FOR XML RAW('AssignTree'), TYPE, ROOT('AssignTrees'))), '')
此处使用的变量: @TRUE = 1 @FALSE = 0
您在查询中看到的这个函数基本上是相同的查询,但Where子句是这样的:WHERE ATree.ParentID = @inputParentID
编辑:
Case语句仅适用于直接引用AssignDetails中的行的子AssignTree。 Parent AssignTree没有与之关联的AssignDetails行。所以我需要计算它的'IsSelected&#39;孩子的价值。这是我在这里没有处理的部分(我不知道如何)。
答案 0 :(得分:0)
以下代码永远不会评估为null
CASE
WHEN EXISTS ( SELECT 1
FROM ObjAssign obj WITH(NOLOCK)
WHERE obj.[Object] = 'OBJ1'
AND obj.Assign = ATree.Assign)
THEN @TRUE
ELSE @FALSE
END AS IsSelected,
基于您的问题,您似乎希望它能够这样做。
还有什么是@TRUE和@FALSE?