Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Insert Column Names of a table into rows of anothr

Author  Topic 

adonnell
Starting Member

4 Posts

Posted - 2011-08-17 : 12:18:51
I have a table with 75 columns and I want to take just the column names and insert them into rows of a different table. One row for each of the old column names, with the target column name being Attribute.

it would be something like

columns in original table: a b c d e f g
looking for:
Attribute
a
b
c
d
e
f
g


I was looking at pivot and unpivot but it looks like that includes the data in the original columns. I only want column names.

Any help would be great!

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-08-17 : 12:27:33
insert into YourNewTable
select COLUMN_NAME
from INFORMATION_SCHEMA.COLUMNS
where TABLE_NAME = 'OriginalTable'

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

adonnell
Starting Member

4 Posts

Posted - 2011-08-17 : 12:30:19
nice! I added
insert into YourNewTable (newcolumn name)

thank you for the help!

quote:
Originally posted by tkizer

insert into YourNewTable
select COLUMN_NAME
from INFORMATION_SCHEMA.COLUMNS
where TABLE_NAME = 'OriginalTable'

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-08-17 : 12:44:23
You're welcome, glad to help.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -