Here is a little sample of a function to get multiple values from a database into a comma delimited string.
Code:
--Variable Declaration
DECLARE @Count int
DECLARE @i int
DECLARE @String varchar(8000)
DECLARE @NameString varchar(100)
-- A table to store all the results that match criteria. Change the criteria by altering the select statement.
DECLARE @NameTable TABLE
(
[id] int IDENTITY(1,1),
[engineer] varchar(100)
)
INSERT INTO
@NameTable
SELECT ENGINEER
FROM tbl_engineer
WHERE (ACTIVE = 1) AND (HASHHT = 1) AND (RDTLOGGEDON = 0)
SET @i = 1 -- initialize the counter to increment
SET @Count = (SELECT COUNT(*) FROM @NameTable) -- determine when to stop the loop
SET @String = '' -- a starting value for the string to return.
-- While loop... while there are still records in the table, add the engineer to the string.
WHILE @i <= @Count
BEGIN
SET @NameString = (SELECT engineer FROM @NameTable WHERE id = @i)
SET @String = @String + @NameString + ', '
SET @i = @i +1
END
-- Remove the last comma from the string.
SELECT SUBSTRING(@String, 1, LEN(LTRIM(RTRIM(@String))) - 1)
There are other ways of course, but I thought this might help any of you out there that need to do this.