0

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;
joshi123
  • 835
  • 2
  • 13
  • 33
  • can you add any code you have tried. Please read through [Tips for asking a good Structured Query Language (SQL) question](https://meta.stackoverflow.com/questions/271055/tips-for-asking-a-good-structured-query-language-sql-question/271056) – Andrei Nov 29 '17 at 15:20
  • pseudo code added – joshi123 Nov 29 '17 at 15:33

0 Answers0