Solved

SQL While Loop to concatenate fields of two different records

Posted on 2008-06-10
5
2,450 Views
Last Modified: 2008-06-10
I have the attached and SP code snippet which, when fed a VCHRNMBR variable, will return up to 4 rows.  I need to design a WHILE Loop to combine the DistRef field from all the returned records and concatenate them in one field, delimited by a comma.  I am new to working with WHILE loops, how would I go about this?
SELECT coalesce(pm1.DistRef+',','')+ pm1.DistRef as 'TEXT'
from PM20000 pm2
join PM10100 pm1 ON pm2.VCHRNMBR=pm1.VCHRNMBR
where @VCHRNMBR = pm2.VCHRNMBR

Open in new window

0
Comment
Question by:ilibhart80
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 16

Accepted Solution

by:
brad2575 earned 500 total points
ID: 21751242
This is in an SP I created to get all the values from a query and put them in one string and return that string:

This will return a comma delimited string of all the values that the select query has in it.

This is basically the full stored procedure minus the create and the return portions.

If you have any questions let me know.
Begin
	SELECT @OUTPUT = @OUTPUT + case when @OUTPUT = '' then '' else ', '  end + rtrim(T1.SelectField)		
	FROM Table1 T1 INNER JOIN
	Table2 T2 ON T1.Key = T2.Key 
	WHERE (T2.lookUpfield = @VariablePassedToSQ) 	
	Group by T1.SelectField
	Order by T1.SelectField
    End

Open in new window

0
 
LVL 19

Expert Comment

by:frankytee
ID: 21751272
is your db sql server or Access or something else?
0
 
LVL 19

Expert Comment

by:frankytee
ID: 21751281
oh i missed the sp, go with brad
0
 

Author Comment

by:ilibhart80
ID: 21751725
Brad:

I think this is going to do the trick, just two issues

1.  The SP wants input for the @output paramter.  How to I make the SP look for only the ID parameter.

2.  When i feed the SP a valid ID, i get the following error:
Syntax error converting the nvarchar value 'i.libhart, AP' to a column of data type int.  

when i feed 'test' to the @output parameter and the other two are valid
0
 
LVL 16

Expert Comment

by:brad2575
ID: 21752132
For #1 the "Output" paramater is just the variable that is set up to hold the string of data that is being generated and that is going to be returned from the stored procedure.

For #2 it sounds like you are passing a text value to the SP but in the SP itself the variable is declared as an int and it does not like that.

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As they say in love and is true in SQL: you can sum some Data some of the time, but you can't always aggregate all Data all the time! Introduction: By the end of this Article it is my intention to bring the meaning and value of the above quote to…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question