Any guidance appreciated, even if it's a different way to think the problem.
Redesign dataTable to be a key/value table like so...
create table dataTable ( cid integer not null, key varchar(255) not null, value int, primary key(cid, key););insert into #DataTable(cid, key, value)values (23, 'col1', 1), (23, 'col2', 2), (23, 'col3', 3), (23, 'col4', 4), (23, 'col5', 37);
Then join and select directly by key.
select dt.*from ColumnTable ct left join DataTable dt on dt.cid = ct.cid and dt.key = ct.ColumnName
This way you can have any keys you want, as many as you want.
If DataTable is already in use, you can create a view for backwards compatibility.