Dear All,
Im trying to collect values from a query into a single variable within a loop, like so:
WHILE condition is true
BEGIN
SET @.intLoop = @.intLoop + 1
@.myString = @.myString + , + (SELECT companyName FROM @.tblTheseComp WHERE id = @.intLoop
END
For some reason though the @.myString does not collect up the values, but will equal NULL at the end of the loop.
If however I simple do
WHILE condition is true
BEGIN
SET @.intLoop = @.intLoop + 1
@.myString = (SELECT companyName FROM @.tblTheseComp WHERE id = @.intLoop
END
Then I get the last value from the query as expected.
Can anyone explain why this might be?
Thanks in advance!Hi
Your variable is null initially -
NULL +'Something' = NULL
HTH|||Also - you don't need a loop:
DECLARE @.myString AS VarChar(1000)
SELECT @.myString = ''
SELECT @.myString = @.myString + ', ' + companyName
FROM @.tblTheseComp
SELECT @.myString = SUBSTRING(@.myString, 3, LEN(@.myString)-2)
SELECT @.myString|||This code will handle if any of the companyName is null.
DECLARE @.myString AS VarChar(1000)
SELECT @.myString = ''
SELECT @.myString = @.myString + coalesce(companyName+',','')
FROM @.tblTheseComp
SELECT @.myString = SUBSTRING(@.myString, 1, LEN(@.myString)-1)
SELECT @.myString|||Nice one thanks a lot! A final obsticle and I'm there!
As I need to send a list of action items to my users, I need to add a carriage return at the end of each line, any idea how I would do that, the mail is sent as a text file so I assume I'll need the CTRL + M combination or in ASCII:
Char: CR, Oct:15, Dec:13, Hex:d
Any ideas anyone?|||Hmm wasnt that tricky I needed CHAR(13) + CHAR(10) cool, thanks for all the help again!|||add char(13) with ur string.
eg:
PRINT 'First line.' + CHAR(13) + CHAR(10) + 'Second line.'|||Thanks yes I noticed I had to include a string to add CHAR(13) + CHAR(10) to. Cheers!
No comments:
Post a Comment