Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have 3 tables
1.AuxInfo
aux id AuxName
101 aux1
102 aux2
103 aux3

2.AuxProduct

auxProdId ProductId AuxId
auxprod1 p1 101
auxprod2 p1 102
auxprod3 p2 101
auxprod4 p2 103

3.Product

ProductId ProductName
p1 test1
p2 test2

I want to write a stored procedure which will show the result as shown below.

ProductId ProductName AuxNameCSV
p1 test1 aux1,aux2
p2 test2 aux1,aux3

I am trying to convert the auxnames into CSV format for each productid

Can anybody help
Posted
Comments
Tomas Takac 9-Dec-14 4:25am    
What you need is an aggregate function to concatenate the values. Unfortunatelly SQL server doesn't have one. However there are several alternatives[^].
Jasmin_jey 9-Dec-14 4:43am    
I have written a query like this.
SELECT ap.productguid,SUBSTRING((SELECT ',' + ai.Name
FROM AuxInfo ai, AuxProduct ap
WHERE ai.AuxiliaryId=ap.AuxiliaryId and
ap.IsActive=1
FOR XML PATH('')),2,200000) AS CSV
FROM AuxProduct ap ;

then it is displaying all the productid's with all the aux names lsited as CSV as shown below.
productid productName AuxNameCSV
p1 test1 aux1,aux2,aux3
p2 test2 aux1,aux2,aux3

1 solution

Fill the result in a Datatable and Export it to a CSV file..
See ..
Writing a DataTable to a CSV file[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900