简体   繁体   中英

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 age 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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM