Solved

SQL While Loop to concatenate fields of two different records

Posted on 2008-06-10
5
2,446 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
  • 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

809 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