SQL results to string with wildcard
08:31 23 May 2012

Suppose you have a table like this:

ID     FNAME      LNAME
1      Bob        Smith
2      Sally      Jones

A simple SELECT * FROM [Table] will return all rows. But what if you wanted to build a single string out of the results, and the column names are unknown? In other words, this will not work:

SELECT ID + ',' + FNAME + ',' + LNAME FROM [Table]

because you don't know the column names. Additionally, COALESCE won't work because it doesn't accept wildcards. Ideally you want to execute something like this:

SELECT dbo.FunctionThatSplitsResultsToString(*) FROM [Table]

and have it return

1,Bob,Smith
2,Sally,Jones

Is this possible?

sql sql-server