Simple Example of COALESCE Function:
Created one Temp table in which i have put one 9 rows with the help of WHILE loop.
The at the Main part i have just take Column to COALESCE function.
DROP TABLE #Material
SET NOCOUNT ON
CREATE TABLE #Material
(
MaterialID INT
)
DECLARE @LoopCounter INT
DECLARE @MaxLoopCounter INT
SET @LoopCounter = 1
SET @MaxLoopCounter = 10
WHILE (@LoopCounter < @MaxLoopCounter)
BEGIN
INSERT INTO #Material (MaterialID) VALUES (@LoopCounter)
SET @LoopCounter = @LoopCounter + 1
END
/* MAIN PART */
DECLARE @MaterialID VARCHAR(100)
SELECT @MaterialID = COALESCE(@MaterialID + ',','') + CAST(MaterialID AS VARCHAR(100)) FROM #Material
PRINT 'FINAL OUTPUT: '+ @MaterialID
-- SELECT * FROM #Material
SET NOCOUNT OFF