on 2022 Jul 12 8:34 AM
I'm using SQL anywhere 17.0.11 build 6933.
I'm using the SQL Anywhere 17 demo database to explain what I'm trying to achieve.
I created the following query to generate an XML line per customer:
select xmlconcat(xmlelement(name Customer, (select xmlconcat(XMLelement(name Name,CompanyName) ,XMLelement(name City,City) ,(select SurName ||', '|| GivenName as FullName from groupo.Contacts where CustomerID = groupo.Customers.Id for xml auto, elements) ) ) ) ) from groupo.Customers where Id between 117 and 119For very customer I get a line returned in the format I'm lookgin for:
<textarea rows="3" cols="180"> <customer><name>Square Sports</name><city>Unionville</city></customer> <customer><name>Raleigh Active Wear</name><city>Chandler</city></customer> <customer><name>Ocean Sports</name><city>Owen Sound</city><contacts><fullname>Hildebrand, Jane</fullname></contacts></customer> </textarea>So far so good. But now I want these lines added into 1 XML string surrounded by Customers.
<customers> <customer> <name>Square Sports</name> <city>Unionville</city> </customer> <customer> <name>Raleigh Active Wear</name> <city>Chandler</city> </customer> <customer> <name>Ocean Sports</name> <city>Owen Sound</city> <contacts><fullname>Hildebrand, Jane</fullname></contacts> </customer> </customers>Anyone any idea how to achieve this?
Request clarification before answering.
I don't know whether this is a good solution, the XML functions are still something I usually have to fiddle around with...- but XMLAGG is used to generate the sequence of customer nodes, and an outer XMLELEMENT is required for the root node:
select xmlelement(name Customers, xmlagg(xmlconcat(xmlelement(name Customer, (select xmlconcat(XMLelement(name Name,CompanyName) ,XMLelement(name City,City) ,(select SurName ||', '|| GivenName as FullName from groupo.Contacts where CustomerID = groupo.Customers.Id for xml auto, elements) )))))) from groupo.Customers where Id between 117 and 119
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks a lot. This seems to be the solution.
I've been fiddling around for a day to get what I already had but couldn't figure out the last step.
I'll adjust my queries in production and see what it will do.
Thanks a lot.
User | Count |
---|---|
96 | |
11 | |
9 | |
9 | |
7 | |
5 | |
4 | |
4 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.