Generate XML file in Sql Server in a different way -


i have created 2 tables employer , reportpage.

the employerid common field 2 tables. want generate xml tables. write queries , execute.

the query here:

select e.employername employername,         e.employerid employerid, ( select       rp.employerid employerid,       rp.pagenumber pagenumber,        rp.title title,        rp.score score        reportpage rp  rp.employerid = e.employerid        xml auto, type, elements)        employer e employerid = 09340 xml auto, type, elements, root       ; 

it gives output xml this:

<?xml version = '1.0' encoding = 'utf-8' standalone = 'no'?>     <root>  <e>     <employername>company, inc.</employername>     <employerid>09340</groupnumber>       <rp>           <employerid>09340</employerid>           <pagenumber>1</pagenumber>           <title>executive summary</title>               <score>67</score>       </rp>    </e>     <e>     <employername>company, inc.</employername>     <employerid>09340</groupnumber>       <rp>           <employerid>09340</employerid>           <pagenumber>2</pagenumber>           <title>executive summary</title>               <score>75</score>       </rp>    </e>      <e>     <employername>company, inc.</employername>     <employerid>09340</groupnumber>       <rp>           <employerid>09340</employerid>           <pagenumber>3</pagenumber>           <title>executive summary</title>               <score>80</score>       </rp>    </e>     </root> 

but expected form of xml output:

<?xml version = '1.0' encoding = 'utf-8' standalone = 'no'?>     <root>        <e>           <employername>company, inc.</employername>           <employerid>09340</groupnumber>        </e>       <rp> <employerid>09340</employerid>           <pagenumber>1</pagenumber>           <title>executive summary</title>               <score>67</score>       </rp>            <rp> <employerid>09340</employerid>           <pagenumber>2</pagenumber>           <title>common</title>              <score>75</score>       </rp>            <rp> <employerid>09340</employerid>           <pagenumber>3</pagenumber>           <title>physical</title>                <score>80</score>       </rp>        </root> 

could me that? have tried long time. thanks!

applying instructions article "belle’s sql musings: sqlxml: how join multiple xml snippets (using query() , union all)" on problem gave me code:

declare @employerid varchar(5) = '09340'  declare @equery xml declare @rpquery xml  set @equery = (   select     e.employername employername,      e.employerid employerid       employer e       employerid = @employerid   xml auto, type, elements, root )  set @rpquery = (   select     rp.pagenumber pagenumber,      rp.title title,      rp.score score       reportpage rp       rp.employerid = @employerid   order     rp.pagenumber   xml auto, type, elements, root )  select   @equery.query('//e') union select   @rpquery.query('//rp') xml path (''), root('root'), type 

you can check results online in sql fiddle: http://sqlfiddle.com/#!6/e95b5/7/0


disclaimer: i'm no t-sql expert, query might improved bit , magic keywords thrown away, hacked using google without knowing (and why)


Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -