SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


XML Question (?)


XML Question (?)

Author
Message
btio_3000
btio_3000
SSC-Enthusiastic
SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)

Group: General Forum Members
Points: 193 Visits: 491
Hi,

Let's say I have the following table with just 1 record:

SELECT Field1, Field2, Field3 FROM tbl

returns 11, 22, 33

What would be a query to return something like:

<CustomInformation>
<ColumnValue name="Field1">11</ColumnValue>
<ColumnValue name="Field2">22</ColumnValue>
<ColumnValue name="Field3">33</ColumnValue>
</CustomInformation>

?

Thanks!
Mark Cowne
Mark Cowne
Hall of Fame
Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)

Group: General Forum Members
Points: 3101 Visits: 24082
SELECT 
(SELECT 'Field1' AS "@name", Field1 AS "text()" FOR XML PATH('ColumnValue'),TYPE),
(SELECT 'Field2' AS "@name", Field2 AS "text()" FOR XML PATH('ColumnValue'),TYPE),
(SELECT 'Field3' AS "@name", Field3 AS "text()" FOR XML PATH('ColumnValue'),TYPE)
FROM tbl
FOR XML PATH('CustomInformation')



____________________________________________________

Deja View - The strange feeling that somewhere, sometime you've optimised this query before

How to get the best help on a forum

http://www.sqlservercentral.com/articles/Best+Practices/61537




btio_3000
btio_3000
SSC-Enthusiastic
SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)

Group: General Forum Members
Points: 193 Visits: 491
Beautiful, thanks so much!
btio_3000
btio_3000
SSC-Enthusiastic
SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)

Group: General Forum Members
Points: 193 Visits: 491
Journey goes on...

Now, what if I have two records and need something like

<instances>

<instance instanceCode1="Rec1_Code1" instanceCode2="Rec1_Code2">
....<CustomInformation>
........<ColumnValue name="Field1">11</ColumnValue>
........<ColumnValue name="Field2">22</ColumnValue>
........<ColumnValue name="Field3">33</ColumnValue>
....</CustomInformation>
</instance>

<instance instanceCode1="Rec2_Code1" instanceCode2="Rec2_Code2">
....<CustomInformation>
........<ColumnValue name="Field1">1111</ColumnValue>
........<ColumnValue name="Field2">2222</ColumnValue>
........<ColumnValue name="Field3">3333</ColumnValue>
....</CustomInformation>
</instance>

</instances>

?

I figured the external <instances>, the internal <instance>.... Totally stuck

Thanks in advance as always
Mark Cowne
Mark Cowne
Hall of Fame
Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)

Group: General Forum Members
Points: 3101 Visits: 24082
Not sure of your table structure, but this give the correct results


DECLARE @tbl TABLE(Code1 VARCHAR(10), Code2 VARCHAR(10), Field1 INT, Field2 INT, Field3 INT)
INSERT INTO @tbl(Code1,Code2,Field1,Field2,Field3)
VALUES ('Rec1_Code1','Rec1_Code2',11,22,33),
('Rec2_Code1','Rec2_Code2',1111,2222,3333);

SELECT
Code1 AS "@instanceCode1",
Code2 AS "@instanceCode2",
(SELECT
(SELECT 'Field1' AS "@name", Field1 AS "text()" FOR XML PATH('ColumnValue'),TYPE),
(SELECT 'Field2' AS "@name", Field2 AS "text()" FOR XML PATH('ColumnValue'),TYPE),
(SELECT 'Field3' AS "@name", Field3 AS "text()" FOR XML PATH('ColumnValue'),TYPE)
FOR XML PATH('CustomInformation'),TYPE)
FROM @tbl
FOR XML PATH('instance'),Root('instances');



____________________________________________________

Deja View - The strange feeling that somewhere, sometime you've optimised this query before

How to get the best help on a forum

http://www.sqlservercentral.com/articles/Best+Practices/61537




btio_3000
btio_3000
SSC-Enthusiastic
SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)

Group: General Forum Members
Points: 193 Visits: 491
Ok, getting there:-)

works fine, thank you once again
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