Unanswered: Loop through fields in a table, add to variable.
can anyone help me with the following problem?
I am trying to build up a string from values from a field in a table. The variable MyValInt should take the value from MyField and loop through the table, adding a comma and a space each time to create a string at the end which displays all the values in one string. I will then use len, left to shave the last two characters off at the end to tidy it up.
The code I have so far is below, when I run it it states it has completed successfully but does not display the end result so i'm not sure what it is doing.
Cany anyone please assist?
DECLARE @MyValInt int
DECLARE @MyValStr as nvarchar(255)
while @MyValInt is not null
SELECT @MyValInt = MyField
SET @MyValStr = @MyValInt + ', ' + @MyValInt
The issue i'm facing now is that 'Myfield' needs to be set as a int before it is placed into the string or the value of the field is 3.00111e+006.
I have been playing around with the code but no joy so far.
I'm wondering if it is possible to brign in MyField as an int, assign it to the first variable then use that to add the comma and the next value before setting it to the second variable which is then a string DataType?
Thanks again for your help, i'll keep you posted if I crack it.