i need to collect all return data into a variable using comma separated.
let say i have a select command like: select * from #temptable.
it’s return:
Field1|Field2
-------------
Value1|Value2
Expected Result: @testvariable hold the value: ‘Value1′,’Value2’
On this table their may have 2 columns and i need to store all the return result into a single variable. We can easily collect a single value like: select @var=column1 from #temptable. But i need to store all.Here the problem is, the number of column can be vary. Mean, number of column and name of column generate from another query.So, i can’t mention the field name.I need a dynamic way to do it. on this table only one row will be return. Thanks in advance.
You can do this without dynamic SQL using
XMLThis can probably be simplified by someone more adept at XML querying than me.