Вопрос

I have this query:

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')
    ;

It returns this 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>

But I need that col_value elements must be like following:

<col_value>341</col_value>

So, how can I modify the query to reach the following result?

<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>

Thanks everybody. Charles.

Это было полезно?

Решение

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

Другие советы

try change your query like this:

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')
;

I hope this helps.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top