Solved

SQL While Loop to concatenate fields of two different records

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Access 2003 query, round down to nearest 5 cents 3 22
Access left join query 5 31
encyps queries mssql 15 27
sql query Help 12 34
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…
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…
This video discusses moving either the default database or any database to a new volume.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

747 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now