kurupt_89
kurupt_89

Reputation: 1592

Create temp table with column names from another tables column data

Is it possible to create a temp table with columns named with data from a single column in another table for example -

singleColumn
-data1
-data2
-data3
-data4

then from this create a temp table like -

data1 | data2 | data3 | data4

edit:

I have a user that can have 0 to whatever demographics from the demographics table. A join will return several rows for a user. I need this data joined on 1 row so I can store this data in CSV file.

Upvotes: 1

Views: 3118

Answers (2)

kurupt_89
kurupt_89

Reputation: 1592

Don't think its possible to return data the way I would like so ended up doing doing it with 2 DataTables.

Upvotes: 0

user596075
user596075

Reputation:

declare @dynamicSql varchar(1000)
set @dynamicSql = 'create table #yourTempTable ('

select @dynamicSql += yourDataCol + ' nvarchar(100) not null, '
from TestTempTables

set @dynamicSql = LEFT(@dynamicSql, len(@dynamicSql) - 1) + ')'

exec(@dynamicSql)

Upvotes: 1

Related Questions