Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Problem using FOR XML in SQL Query


Problem using FOR XML in SQL Query

Author
Message
spungus
spungus
Grasshopper
Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)

Group: General Forum Members
Points: 16 Visits: 140
Hi to all,

I have one format problem that i cannot get it right. I hope you can help me.
So here it is.



declare @test table(id int identity(1,1), custname varchar(10), custcode varchar(10) )

insert @test
values ('name1','code1'),('name2','code2')

SELECT
(SELECT custname,custcode FOR XML PATH(''),TYPE) AS Item
FROM @test
FOR XML PATH('Header'),ROOT('ns1')




The above code it will output like this.

<ns1>
<Header>
<Item>
<custname>name1</custname>
<custcode>code1</custcode>
</Item>
</Header>
<Header>
<Item>
<custname>name2</custname>
<custcode>code2</custcode>
</Item>
</Header>
</ns1>




What i want is something like this...

<ns1>
<Header>
<Item>
<custname>name1</custname>
<custcode>code1</custcode>
</Item>
<Item>
<custname>name2</custname>
<custcode>code2</custcode>
</Item>
</Header>
</ns1>




All of your answers is very much appreciated. thanks
Matthew Darwin
Matthew Darwin
SSC-Enthusiastic
SSC-Enthusiastic (197 reputation)SSC-Enthusiastic (197 reputation)SSC-Enthusiastic (197 reputation)SSC-Enthusiastic (197 reputation)SSC-Enthusiastic (197 reputation)SSC-Enthusiastic (197 reputation)SSC-Enthusiastic (197 reputation)SSC-Enthusiastic (197 reputation)

Group: General Forum Members
Points: 197 Visits: 874
Try this:-

SELECT 
(SELECT custname AS CustName
, custcode AS CustCode
FROM @test t1 FOR XML PATH('Item'), TYPE)
FOR XML PATH('Header'),ROOT('ns1')



Follow me on twitter @EvoDBA

Check out my blog Natural Selection DBA
spungus
spungus
Grasshopper
Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)

Group: General Forum Members
Points: 16 Visits: 140
Matthew Darwin (7/3/2013)
Try this:-

SELECT 
(SELECT custname AS CustName
, custcode AS CustCode
FROM @test t1 FOR XML PATH('Item'), TYPE)
FOR XML PATH('Header'),ROOT('ns1')



Thanks Matthew Darwin! i see how it is construct.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search