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.
| Author |
Topic |
|
Lady
Starting Member
32 Posts |
Posted - 2002-11-21 : 10:38:02
|
| in NorthWind databasehow shell I get xml like this:<Customer CustomerID="ALFKI"> <orders> <Order OrderID="10643" /> <Order OrderID="10692" /> <Order OrderID="10702" /> <Order OrderID="11011" /> </orders></Customer><Customer CustomerID="ANATR"><orders> <Order OrderID="10308" /> <Order OrderID="10625" /> </orders></Customer>in books online we have following tsql sample:SELECT 1 as Tag, NULL as Parent, Customers.CustomerID as [Customer!1!CustomerID], NULL as [Order!2!OrderID]FROM CustomersUNION ALLSELECT 2, 1, Customers.CustomerID, Orders.OrderIDFROM Customers, OrdersWHERE Customers.CustomerID = Orders.CustomerIDORDER BY [Customer!1!CustomerID], [Order!2!OrderID]FOR XML EXPLICITthis example without bold <orders> tag. I have to get it!!! exerything is OK, but how shell I get <orders> tag? |
|
|
rrb
SQLTeam Poet Laureate
1479 Posts |
Posted - 2002-11-21 : 17:12:02
|
Hi Lady, I suspect you don't actually need the <orders> tag - but if you do, I think your best bet is probably going to be xsldid you know you can get the same result byselect Customers.CustomerID, Orders.OrderID from Customers, OrdersWHERE Customers.CustomerID = Orders.CustomerID order by customers.customerid, Orders.OrderID for xml autoquote: I have to get it!!!
Can you explain why you have to have it?PS - you might want to have a look at [url]http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=21827[/url]--I hope that when I die someone will say of me "That guy sure owed me a lot of money"Edited by - rrb on 11/21/2002 17:20:01 |
 |
|
|
monkeybite
Posting Yak Master
152 Posts |
Posted - 2002-11-21 : 19:02:49
|
This'll do what you want, i think:SELECT 1 AS [tag], NULL AS [parent], c.CustomerID AS [Customer!1!CustomerID], NULL AS [orders!2!], NULL AS [Order!3!OrderID]FROM dbo.Customers AS [c]UNION ALLSELECT 2 AS [tag], 1 AS [parent], o.CustomerID AS [Customer!1!CustomerID], NULL AS [orders!2!], NULL AS [Order!3!OrderID]FROM dbo.Orders AS [o]GROUP BY o.CustomerIDUNION ALLSELECT 3 AS [tag], 2 AS [parent], o.CustomerID AS [Customer!1!CustomerID], NULL AS [orders!2!], o.OrderID AS [Order!3!OrderID]FROM dbo.Orders AS [o]ORDER BY [Customer!1!CustomerID], [tag], [parent]FOR XML EXPLICIT There's two things to notice here.First, to get the sorting and grouping correct, there must be a key field appearing in all of the union statements, in this case it is CustomerID.Second, to append the correct number of <orders/> tags, simply select the key value from the orders tables, and use GROUP BY to get only one per customer. Finally, sort by your key value ([CustomerID]), then [tag] and [parent] to get the nesting correct.-- monkey-- edited the [orders!2!] tag to the correct case!-- edited again and added an explanation to the solutionEdited by - monkeybite on 11/21/2002 19:05:08Edited by - monkeybite on 11/21/2002 19:11:34 |
 |
|
|
|
|
|
|
|