I have a table #temp
with certain columns. Each column corresponds to a column name in another table, #temp2
.
I'd like to do select colA,colB,colC from #temp2'
where colA,colB,colC are all columns in #temp
.
The problem is that the col names in #temp
can change dynamically, and I don't want to hard code the column names into the select query. Is there a way to do this in T-SQL?
Something like,
declare @columnList varchar(max);
set @columnList =
(select (COLUMN_NAME) from INFORMATION_SCHEMA.columns where TABLE_NAME = '#temp');
select @columnList from #temp2;