Solved

SQL While Loop to concatenate fields of two different records

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

910 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

17 Experts available now in Live!

Get 1:1 Help Now