Quantcast
Channel: User Schwern - Stack Overflow
Viewing all articles
Browse latest Browse all 581

Answer by Schwern for Getting Table Column Value Based on Dynamic Column Name

$
0
0

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.


Viewing all articles
Browse latest Browse all 581

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>