开发者

Insert in a temporal table the names of another table SQL Server 2008

开发者 https://www.devze.com 2023-02-12 00:57 出处:网络
Is there a way to have something like: id NameColumn -------------- 1sex 2age 3weight 4height ...from a known table:

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

精彩评论

暂无评论...
验证码 换一张
取 消