Simple T-SQL statement to create a list of lookup values
This method allows to create a comma separated list from a set of row values without using the dreaded cursor.
-- Declare & initialize the CommaList variable. DECLARE @CommaList VARCHAR(max) SET @CommaList= '' -- This select does the work SELECT @CommaList = ISNULL(@CommaList,'') + ISNULL(StatusDesc,'') + ',' FROM ListTable -- Remove the trailing ', ' characters. SET @CommaList = SUBSTRING(@CommaList, 1, LEN(@CommaList)-1) -- output our comma separated string. print @CommaList