Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 FOR XML PATH with multiple selects

Author  Topic 

misterish
Starting Member

10 Posts

Posted - 2011-12-09 : 23:46:52
Hi,
I am trying to make nest the results of two selects into one XML structure using FOR XML PATH, but can not figure it out. What I am looking for is this:


<data>
<registrants>
<registrant>...</registrant>
<registrant>...</registrant>
</registrants>
<pageviews>
<total>99999</total>
</pageviews>
</data>


My two queries would be:


SELECT registrant_id
FROM registrant
FOR XML PATH('registrants'), TYPE

SELECT COUNT(pageview_id) AS total
FROM pageviews
FOR XML PATH('pageviews'), TYPE


How can I merge these two into one XML block like above?

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-12-10 : 08:04:41
[code]
SELECT *
FROM
(SELECT registrant_id
FROM registrant
FOR XML PATH('registrant'), TYPE
)a(registrants),
(
SELECT COUNT(pageview_id) AS total
FROM pageviews
FOR XML PATH('pageview'), TYPE
)b(pageviews)
FOR XML PATH('data')
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

johntech
Yak Posting Veteran

51 Posts

Posted - 2011-12-13 : 09:01:32
ok show this example

TYPE,GROUP,VALUE
Books,Hardback,56
Books,Softcover,34
CDs,Singles,45
CDS,Multis,78
The output style I need is:

<data>
<variable name="TYPE">
<row>
<column>GROUP</column>
<column>VALUE</column>
</row>
<row>
<column>GROUP</column>
<column>VALUE</column>
</row>
</variable>
<variable name="TYPE">
<row>
<column>GROUP</column>
<column>VALUE</column>
</row>
<row>
<column>GROUP</column>
<column>VALUE</column>
</row>
</variable>
</data>
Edit: As far as I can tell I require the multiple values. I'm generating XML for use with Xcelsius (Linking XML and Xcelsius) so have no control over in the formatting of the XML. I can generate the XML using ASP as per the linked tutorial, but I was hoping to get it straight from SQL Server.

Edit 2: I was hoping for something elegant and tidy... but Godeke's example got the closest. Some fiddling with the SQL and I've come up with:

select
"type" as '@name',
"group" as 'row/column',
null as 'row/tmp',
"value" as 'row/column'
from tableName
for xml path('variable'), root('data')

original post by
http://stackoverflow.com/questions/251957/ms-sql-2005-table-to-xml
Go to Top of Page

misterish
Starting Member

10 Posts

Posted - 2011-12-13 : 10:53:34
Thank you, visakh! That was it.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-12-13 : 10:54:12
wc

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -