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


FOR XML PATH with multiple attributes in child nodes.


FOR XML PATH with multiple attributes in child nodes.

Author
Message
Dennis Post
Dennis Post
Old Hand
Old Hand (397 reputation)Old Hand (397 reputation)Old Hand (397 reputation)Old Hand (397 reputation)Old Hand (397 reputation)Old Hand (397 reputation)Old Hand (397 reputation)Old Hand (397 reputation)

Group: General Forum Members
Points: 397 Visits: 547
Trying to get the hang of querying to XML.

Although I can get the result I'm looking for, I'm not sure if it's the recommended way.

Here's all the test code.
/*
CREATE TABLE Customers
(
CustID TinyInt Identity(1,1) NOT NULL,
Name Varchar(30) NOT NULL,
Address1 Varchar(50) NOT NULL,
Address2 Varchar(50) NOT NULL,
Postcode Varchar(10) NOT NULL,
Country TinyInt NOT NULL,
CustGrpID TinyInt NOT NULL
)

CREATE TABLE CustomerGroups
(
CustGrpID TinyInt IDENTITY(1,1) NOT NULL,
Name Varchar(20) NOT NULL
)

INSERT Customers
(Name, Address1, Address2, Postcode, Country, CustGrpID)
SELECT 'Cust_One', 'Far', 'Far away', '90210', 255, 1 UNION ALL
SELECT 'Cust_Two', 'Very', 'Very close', '5060', 1, 2 UNION ALL
SELECT 'Cust_Three', 'Right', 'Next door', '4321', 1, 1 UNION ALL
SELECT 'Cust_Four', 'Middle', 'Of nowhere', '666', 127, 2

INSERT CustomerGroups
(Name)
SELECT 'Good' UNION ALL
SELECT 'Bad'
*/

-- Works but the syntax doesn't look "Correct". Shouldn't have to CAST.
SELECT CG.CustGrpID "@CustGrpID",
CG.Name "@CustGrpName"
,CAST((
SELECT C.CustID "@CustID",
C.Name "@CustName",
C.Address1 "Address1",
C.Address2 "Address2"
FROM Customers C
WHERE C.CustGrpID = CG.CustGrpID
FOR XML PATH ('Customer'))
AS XML) --CustList --Gives an extra element
FROM CustomerGroups CG
FOR XML PATH ('CustomerGroup'), ROOT('CustomersPerCustomerGroup')



How should this be written?
Thanks,



For better, quicker answers on T-SQL questions, read Jeff Moden's suggestions.

"Million-to-one chances crop up nine times out of ten." ― Terry Pratchett, Mort
Mark Cowne
Mark Cowne
Hall of Fame
Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)

Group: General Forum Members
Points: 3153 Visits: 24128
Rather than using a cast, replace

FOR XML PATH ('Customer'))

with

FOR XML PATH ('Customer'),TYPE)

____________________________________________________

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




Dennis Post
Dennis Post
Old Hand
Old Hand (397 reputation)Old Hand (397 reputation)Old Hand (397 reputation)Old Hand (397 reputation)Old Hand (397 reputation)Old Hand (397 reputation)Old Hand (397 reputation)Old Hand (397 reputation)

Group: General Forum Members
Points: 397 Visits: 547
Thanks Mark !! Works a charm!



For better, quicker answers on T-SQL questions, read Jeff Moden's suggestions.

"Million-to-one chances crop up nine times out of ten." ― Terry Pratchett, Mort
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