1

我有这个查询:

select f.n, t.l, grp.n, cat.n, col.n, col_value
        from fc 
            inner join f on f.idfc = fc.idfc
            inner join t on t.idf = f.idf
            inner join grp on grp.idt = t.idt
            inner join cat on cat.idgrp = grp.idgrp
            inner join col on col.idcat = cat.idcat
            inner join col_value on col_value.idcol = col.idcol
    for xml auto, root('fc')
    ;

它返回这个 xml:

<fc>
  <f n="341.11.7">
    <t l="A">
      <grp n="ID">
        <cat n="10">
          <col n="R">
            <col_value col_value="341" />
          </col>
          <col n="S">
            <col_value col_value="11" />
          </col>
          <col n="H">
            <col_value col_value="7" />
          </col>
        </cat>
      </grp>
    </t>
  </f>
</fc>

但我需要 col_value 元素必须如下所示:

<col_value>341</col_value>

那么,如何修改查询以达到以下结果?

<fc>
  <f n="341.11.7">
    <t l="A">
      <grp n="ID">
        <cat n="10">
          <col n="R">
            <col_value>341</col_value>
          </col>
          <col n="S">
            <col_value>11</col_value>
          </col>
          <col n="H">
            <col_value>7</col_value>
          </col>
        </cat>
      </grp>
    </t>
  </f>
</fc>

谢谢大家。查尔斯。

4

2 回答 2

0
select
    f.n, t.l, grp.n, cat.n, col.n,
    (select col_value for xml path(''), type)
from fc 
    inner join f on f.idfc = fc.idfc
    inner join t on t.idf = f.idf
    inner join grp on grp.idt = t.idt
    inner join cat on cat.idgrp = grp.idgrp
    inner join col on col.idcat = cat.idcat
    inner join col_value on col_value.idcol = col.idcol
for xml auto, root('fc')

sql fiddle demo

于 2013-10-25T18:17:29.883 回答
0

尝试像这样更改您的查询:

select f.n, t.l, grp.n, cat.n, col.n, (select col_value for xml path,type).query('/row/col_value') )
    from fc 
        inner join f on f.idfc = fc.idfc
        inner join t on t.idf = f.idf
        inner join grp on grp.idt = t.idt
        inner join cat on cat.idgrp = grp.idgrp
        inner join col on col.idcat = cat.idcat
        inner join col_value on col_value.idcol = col.idcol
for xml auto, root('fc')
;

我希望这有帮助。

于 2013-10-25T13:30:35.773 回答