mytable structure: id int, lookuptablename varchar
1, 'lookuptable1'
2, 'lookuptable2'
lookuptable1: id int, item varchar
1, 'item1 from lkt1'
2, 'item2 from lkt1'
lookuptable2: id int, item varchar
1, 'item1 from lkt2'
2, 'item2 from lkt2'
Query:
SELECT GetDelimitedList(lookuptablename) FROM mytable;
Expected result:
1,2~item1 from lkt1,item2 from lkt1
1,2~item1 from lkt2,item2 from lkt2
I have been struggling to find out a way to accomplish this in various ways but just couldn’t figure it out.
A workaround. UDFs won’t work. Maybe CLR, but not native SQL UDF.
First create this proc, which mashes a table into a single row
Then use this SQL batch to produce the output equivalent to
SELECT GetDelimitedList(tablename) FROM mytable;Sample tables used: