Skip to content
Advertisement

How to make a list of T-SQL results with comma’s between them?

Suppose we have a simple query like this:

If we have one record in the result set, I want to set variable @v to that one value. If we have two or more records, I’d like the results to be separated by a comma and a space. What is the best way to write this T-SQL code?

Example:

result set of 1 record:

result set of 2 records:

result set of 3 records:

Advertisement

Answer

this will give you the list of values in a comma separated list

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement