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.[code="sql"]/*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 ALLSELECT 'Cust_Two', 'Very', 'Very close', '5060', 1, 2 UNION ALLSELECT 'Cust_Three', 'Right', 'Next door', '4321', 1, 1 UNION ALLSELECT 'Cust_Four', 'Middle', 'Of nowhere', '666', 127, 2INSERT CustomerGroups (Name)SELECT 'Good' UNION ALLSELECT '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 elementFROM CustomerGroups CGFOR XML PATH ('CustomerGroup'), ROOT('CustomersPerCustomerGroup')[/code]How should this be written?Thanks,
↧