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 2005 Forums
 Transact-SQL (2005)
 FOR XML question

Author  Topic 

littlewing
Starting Member

33 Posts

Posted - 2009-06-10 : 11:29:56
Sorry, this was previously posted in the 2008 forum, but it's for 2005.

I have this FOR XML query returning the following from AdventureWorks:

select top 5
ContactID, Firstname, Lastname, EmailAddress
from Person.Contact
FOR XML RAW ('Row'), ROOT ('File'), ELEMENTS

returns:

<File>
<Row>
<ContactID>1</ContactID>
<Firstname>Gustavo</Firstname>
<Lastname>Achong</Lastname>
<EmailAddress>gustavo0@adventure-works.com</EmailAddress>
</Row>
<Row>
<ContactID>2</ContactID>
<Firstname>Catherine</Firstname>
<Lastname>Abel</Lastname>
<EmailAddress>catherine0@adventure-works.com</EmailAddress>
</Row>
<Row>
<ContactID>3</ContactID>
<Firstname>Kim</Firstname>
<Lastname>Abercrombie</Lastname>
<EmailAddress>kim2@adventure-works.com</EmailAddress>
</Row>
<Row>
<ContactID>4</ContactID>
<Firstname>Humberto</Firstname>
<Lastname>Acevedo</Lastname>
<EmailAddress>humberto0@adventure-works.com</EmailAddress>
</Row>
<Row>
<ContactID>5</ContactID>
<Firstname>Pilar</Firstname>
<Lastname>Ackerman</Lastname>
<EmailAddress>pilar1@adventure-works.com</EmailAddress>
</Row>
</File>

What I really would like is:

<?xml version="1.0" encoding="UTF-8"?>
<File>
<Row>
<Column Name="ContactID">1</Column>
<Column Name="Firstname">Gustavo</Column>
<Column Name="Lastname">Achong</Column>
<Column Name="EmailAddress">gustavo0@adventure-works.com</Column>
</Row>
<Row>
<Column Name="ContactID">2</Column>
<Column Name="Firstname">Catherine</Column>
<Column Name="Lastname">Abel</Column>
<Column Name="EmailAddress">catherine0@adventure-works.com</Column>
</Row>
<Row>
<Column Name="ContactID">3</Column>
<Column Name="Firstname">Kim</Column>
<Column Name="Lastname">Abercrombie</Column>
<Column Name="EmailAddress">kim2@adventure-works.com</Column>
</Row>
<Row>
<Column Name="ContactID">4</Column>
<Column Name="Firstname">Humberto</Column>
<Column Name="Lastname">Acevedo</Column>
<Column Name="EmailAddress">humberto0@adventure-works.com</Column>
</Row>
<Row>
<Column Name="ContactID">5</Column>
<Column Name="Firstname">Pilar</Column>
<Column Name="Lastname">Ackerman</Column>
<Column Name="EmailAddress">pilar1@adventure-works.com</Column>
</Row>
</File>

Can anyone help with the FOR XML clause here? Also can this be saved directly to an XML file without using SSIS?

Thank you.
LW

nathans
Aged Yak Warrior

938 Posts

Posted - 2009-06-10 : 15:22:04
You can try something like this in 2005. First use unpivot to transorm your columns into rows, then use outer for xml query to contruct the desired schema.



declare @x table (ContactId int, FirstName varchar(50), LastName varchar(50), EmailAddress varchar(50))
insert into @x
select 1, 'Gustavo', 'Achong', 'gustavo0@adventure-works.com' union
select 2, 'Catherine', 'Abel', 'catherine0@adventure-works.com' union
select 3, 'Kim', 'Abercrombie', 'kim2@adventure-works.com'

select ( select name [@Name],
value [*]
from ( select ContactId [PK],
cast(ContactID as varchar(50)) [ContactId],
FirstName [FirstName],
LastName [LastName],
EmailAddress [EmailAddress]
from @x
) d
unpivot (value for name in (ContactId, FirstName, LastName, EmailAddress)) t
where PK = x.ContactId
for xml path('Column'), type
)
from @x x
for xml path('Row'), root('File'), type




Nathan Skerl
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-06-10 : 15:32:52
You may want to add an ORDER BY to make sure elements appear in same order as OP request
			where PK = x.ContactId
order by case name
when 'contactid' then 1
when 'firstname' then 2
when 'lastname' then 3
when 'emailaddress' then 4
end

for xml path('Column'), type



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

nathans
Aged Yak Warrior

938 Posts

Posted - 2009-06-10 : 16:52:46
Yea, excellent point peso.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-06-11 : 00:58:34
Thank you.

It seems the UNPIVOT operator puts the records in same the order as the IN clause (try to change the order of IN values and see), but one never knows.

The order of elements should not matter in a XML file, but since OP request is to have same element name for all four elements, and use the attribute instead it may work without the ORDER BY.
But since there are multiple elements with same name at same level, OP have to use some kind of iterative approach, or use the ELEMENT[1], ELEMENT[2] and so on and if so, the order is important.


E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

Luke101
Starting Member

30 Posts

Posted - 2009-06-12 : 16:56:58
wow..

I think he just wants <?xml version="1.0" encoding="UTF-8"?>
at the beginning of the xml file.
Go to Top of Page
   

- Advertisement -