I am writing a stored procedure and have a temp table that is dynmaic in size when it executes. I want to loop through each row in the temp and take a value from a column and then append that value in a varaible that would hold each value from all the rows. I am having trouble doing this. I can see the values, however, I get null for my return. Here is my code and Thanks in advance:
Code:
CREATE TABLE #TEMP
(
ID INT Identity,
ShortDescription INT,
)
INSERT INTO #TEMP
EXEC spRPProjectTemp 99
DECLARE @var As VarChar(MAX)
DECLARE @Counter As INT
SET @Counter = 1
DECLARE @Index AS INT
SET @Index = (SELECT MAX(ID) FROM #TEMP)
DECLARE @varContainer AS Varchar(30)
WHILE @Counter < @Index + 1
BEGIN
SET @varContainer = (SELECT ShortDescription FROM #TEMP WHERE ID = @Counter)
IF @var = ''
BEGIN
SET @var = @varContainer
SET @Counter = @Counter + 1
END
ELSE
BEGIN
SET @var = @var+', '+@varContainer
SET @Counter = @Counter + 1
END
END
SELECT @var AS 'String'
SELECT * FROM #TEMP
Comment