Monday, March 29, 2010

Comma separated string in SQL Server (without cursor)

A simple example of getting a comma seperated string of values from a column in SQL server. As cursors are not recommended (they slow down the process) I am doing it without iterating through rows.

Create PROCEDURE up_GetValueString

AS
BEGIN

DECLARE @statusList varchar(5000)

SELECT @statusList = COALESCE(@statusList + ',', '') +
CAST(unitid AS varchar(50))
from
(
select unitid from product
) a

select @statusList AS IDS

END
GO

No comments:

Post a Comment