I’m trying to split an SQL results set (in a Stored Proc) into multiple columns for XML formatting. I have a results set with a Supplier Number and a Product Number like this:
SupplierID ProdCode
----------- ---------
Supp1 Prod1
Supp1 Prod2
Supp1 Prod3
Supp2 Prod2
Supp2 Prod3
Supp3 Prod2
etc.
I need to split this results set so that each supplier has its own dataset, which can then be split into its own XML node, as shown here:
<SUPPLIER>
<SUPPLIER_LINES>
<SUPPNO>Supp1</SUPPNO>
<PRODCODE>Prod1</WWPROD>
</SUPPLIER_LINES>
<SUPPLIER_LINES>
<SUPPNO>Supp1</SUPPNO>
<PRODCODE>Prod2</WWPROD>
</SUPPLIER_LINES>
<SUPPLIER_LINES>
<SUPPNO>Supp1</SUPPNO>
<PRODCODE>Prod3</WWPROD>
</SUPPLIER_LINES>
</SUPPLIER>
<SUPPLIER>
<SUPPLIER_LINES>
<SUPPNO>Supp2</SUPPNO>
<PRODCODE>Prod2</WWPROD>
</SUPPLIER_LINES>
<SUPPLIER_LINES>
<SUPPNO>Supp2</SUPPNO>
<PRODCODE>Prod3</WWPROD>
</SUPPLIER_LINES>
</SUPPLIER>
I’ll be able to figure out the XML formatting, but am having trouble splitting the results inside a stored proc to get this:
SupplierID ProdCode
Supp1 Prod1
Supp1 Prod2
SupplierID ProdCode
Supp2 Prod2
Supp2 Prod3
You can do a
group byonSupplierIDfor theSUPPLIERnodes and use a correlated sub-query to get theSUPPLIER_LINESnodes.You probably have suppliers in table of it’s own so if you use that table in the main query you don’t have to do the
group by.