SQL: Create a list of columns for a table using T-SQL and STRING_AGG
I often need to create a list of columns for a table. Sometimes it’s because I’m generating scripts but there are other times that I need it for dynamic code.
Let’s use WideWorldImporters as a sample. Imagine I need to get a list of columns for the Sales.Orders table.
The wrong way
I’ve seen people doing this the wrong way. They try to use a SELECT statement that joins a variable to itself like this:
2021-01-05