Skip Navigation Links
Home
Articles
SQL Server 2012
SQL Server 2014
SQL Server 2016
FAQ
Forums
Practice Test
Bookstore
Tip of the Day : How to Create a Comma-Delimited List Without Using a Cursor
SQL Server Helper
Home > > Tip of the Day
SQL Server Helper - Tip of the Day

SQL Server cursors are usually used to process individual rows of data instead of on a complete set of rows.  Cursors let you move through rows returned by a SELECT statement one at a time and perform processing on each row.  Cursors are much slower and should be avoided as much as possible.

Here’s a way of creating a comma-delimited list without the use of a cursor:

DECLARE @CustomerIDs  VARCHAR(8000)

SELECT @CustomerIDs = ISNULL(@CustomerIDs + ',', '') + [CustomerID]
FROM [dbo].[Customers]

SELECT @CustomerIDs

Back to Tip of the Day List Next Tip