XML显式细分和加法问题

时间:2015-08-12 15:49:14

标签: sql ssms ssms-2012

所以我目前编写了一些FOR XML EXPLICIT SQL语句,除了我遇到的两件事情之外它还很顺利

当我尝试添加这个额外的4个标记层时 - 它只是在Chase部分的底部添加了这个PatientData,而不是在每个单独的追逐中删除

如下所示

<ChaseRequests dtGenerated="Friday, January 30, 2015 5:24:39 PM">
  <Chases>
    <Chase id="12345">
      <PracticeId><![CDATA[82791]]></PracticeId>
      <AccountId><![CDATA[8279101]]></AccountId>
      <RequestingCompany><![CDATA[DERP]]></RequestingCompany>
    </Chase>
    <Chase id="12346">
      <PracticeId><![CDATA[82791]]></PracticeId>
      <AccountId><![CDATA[8279101]]></AccountId>
      <RequestingCompany><![CDATA[DERP]]></RequestingCompany>
      <PatientData />
      <PatientData />
    </Chase>
  </Chases>
</ChaseRequests>

正如您所见,患者数据标签显示在底部,我希望在每个ChaseID部分的“RequestingCompany”标签下看到它。

除此之外,我想在“ChaseRequests”标签下插入两个单独的标签

<Vendor Id = 'Derp'/>
<Request Id = 'Herp'/>

但我完全不确定如何做到这一点@ _ @

我的代码到目前为止......

SELECT 
    tag,
    parent,
    [ChaseRequests!1!dtGenerated],
    [Chases!2!],
    [Chase!3!id],
    [Chase!3!PracticeId!CData],
    [Chase!3!AccountId!CData],
    [Chase!3!RequestingCompany!CData],
    [PatientData!4!Element]
FROM (SELECT
    1 Tag,
    NULL Parent,
    'Friday, January 30, 2015 5:24:39 PM' as [ChaseRequests!1!dtGenerated],
    NULL as [Chases!2!],
    NULL as [Chase!3!id],
    NULL as [Chase!3!PracticeId!CData],
    NULL as [Chase!3!AccountId!CData],
    NULL as [Chase!3!RequestingCompany!CData],
    NULL as [PatientData!4!Element]
UNION ALL
SELECT 
    2 Tag,
    1 Parent,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL
UNION ALL
SELECT 
    3 Tag,
    2 Parent,
    NULL,
    NULL,
    c.ChartID,
    '82791',
    '8279101',
    'DERP',
    NULL
FROM Charts c 
WHERE ChartID in (12345,12346)
UNION ALL
SELECT 
    4 Tag,
    3 Parent,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL
FROM Charts c 
WHERE ChartID in (12345,12346)
) X
FOR XML EXPLICIT

我的理想结果......

<ChaseRequests dtGenerated="Friday, January 30, 2015 5:24:39 PM">
    <Vendor ID="Derp"/>
    <Request ID="Herp"/>
  <Chases>
    <Chase id="12345">
      <PracticeId><![CDATA[82791]]></PracticeId>
      <AccountId><![CDATA[8279101]]></AccountId>
      <RequestingCompany><![CDATA[DERP]]></RequestingCompany>
      <PatientData />
    </Chase>
    <Chase id="12346">
      <PracticeId><![CDATA[82791]]></PracticeId>
      <AccountId><![CDATA[8279101]]></AccountId>
      <RequestingCompany><![CDATA[DERP]]></RequestingCompany>
      <PatientData />
    </Chase>
  </Chases>
</ChaseRequests>

仅供参考 - 我计划在该PatientData标签中删除更多嵌套标签 - 我只希望PatientData标签出现在每个“Chase”区域内

任何信息都将不胜感激!

1 个答案:

答案 0 :(得分:1)

FOR XML EXPLICIT按顺序处理行 ,因此您必须对与其层次关系匹配的行强制执行订单。像这样:

SELECT
    tag,
    parent,
    [ChaseRequests!1!dtGenerated],
    [Chases!2!],
    [Chase!3!id],
    [Chase!3!PracticeId!CData],
    [Chase!3!AccountId!CData],
    [Chase!3!RequestingCompany!CData],
    [PatientData!4!Element]
FROM (SELECT 
    1 Tag,
    NULL Parent,
    'Friday, January 30, 2015 5:24:39 PM' as [ChaseRequests!1!dtGenerated],
    NULL as [Chases!2!],
    NULL as [Chase!3!id],
    NULL as [Chase!3!PracticeId!CData],
    NULL as [Chase!3!AccountId!CData],
    NULL as [Chase!3!RequestingCompany!CData],
    NULL as [PatientData!4!Element]
UNION ALL
SELECT 
    2 Tag,
    1 Parent,
    'Friday, January 30, 2015 5:24:39 PM',
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL
UNION ALL
SELECT 
    3 Tag,
    2 Parent,
    'Friday, January 30, 2015 5:24:39 PM',
    NULL,
    c.ChartID,
    '82791',
    '8279101',
    'DERP',
    NULL
FROM Charts c 
WHERE ChartID in (12345,12346)
UNION ALL
SELECT 
    4 Tag,
    3 Parent,
    'Friday, January 30, 2015 5:24:39 PM',
    NULL,
    c.ChartID,
    NULL,
    NULL,
    NULL,
    NULL
FROM Charts c 
WHERE ChartID in (12345,12346)
) X
order by [ChaseRequests!1!dtGenerated],
    [Chases!2!],
    [Chase!3!id],
    Tag
FOR XML EXPLICIT

注意:只有你走得越深,这才会变得越来越复杂。您真的应该考虑切换到PATHAUTO,以便让自己更轻松。