Comma Separated values from table in Sql Server using COALESCE function:
It Returns the Id of All Products based on the Category. The returned Products Ids are Separated by Comma using SQL Server.
Create Procedure GetProductByCategory
@Category nvarchar(50) ,
@ProductIds Varchar(100)
As
Begin
Select @ProductsIds = Coalese(@ProductIds + ',' , ' ') + Cast(ProductId as Varchar(10)) from Product
Where Category=@category
EndExecute the Store Procedure:
Declare @ProductIds Varchar(100)
Exec GetProductByCategory 'Books' , @ProductIds
Select @ProductIds