?
Solved

SQL While Loop to concatenate fields of two different records

Posted on 2008-06-10
5
Medium Priority
?
2,453 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

801 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