Insert in a temporal table the names of another table SQL Server 2008
Is there a way to have something like:
id NameColumn
--------------
1 sex
2 age
3 weight
4 height
...from a known table:
sex ag开发者_Go百科e weight height....
--------------------------
m 12 200 200
f 22 100 150
...
This is because I have like 300 fields so I would like to maker a map table.
Say you have a known table
create table known (sex char(1), age int, weight int, height int)
This gives you the output required
select
[id] = ORDINAL_POSITION,
[NameColumn] = COLUMN_NAME
from INFORMATION_SCHEMA.COLUMNS
where TABLE_NAME = 'known'
Output:
id NameColumn
----------- -----------
1 sex
2 age
3 weight
4 height
If you wanted to create a table out of it, something like
select
[id] = ORDINAL_POSITION,
[NameColumn] = COLUMN_NAME
into #temporal
from INFORMATION_SCHEMA.COLUMNS
where TABLE_NAME = 'known'
精彩评论