Link to home
Start Free TrialLog in
Avatar of OzarkSunshine
OzarkSunshine

asked on

Create table from columns and values in another table.

I have a table, Table1, with several columns and values (columns are different datatypes) ---
COL1      COL2      COL3      COL4      COL5
0                0                 4                 = '3'         1

I want to take both the column name and the value and place into another holding table, Table2 --
COLUMN_NAME      VALUE
COL1                      0
COL2                      0
COL3                      4
COL4                      = '3'
COL5                      1
Avatar of Najam Uddin
Najam Uddin
Flag of United States of America image

select COLUMN_NAME , VALUE
from tab
unpivot
(
  VALUE
  for COLUMN_NAME  in (COL1 ,COL2 ,COL3,COL4,COL5)
) u;

Open in new window

Avatar of OzarkSunshine
OzarkSunshine

ASKER

My initial column names are different, so I want to select all of them and place them and their value in a row in a new table.
Can you please elaborate a bit?
I'm afraid I'm not to familiar with unpivot, so I'm reading up on it now.   I tried your solution above (thinking maybe you are answering my question, it just doesn't make sense to me) but I get the following error :

Msg 156, Level 15, State 1, Line 7
Incorrect syntax near the keyword 'for'.
ASKER CERTIFIED SOLUTION
Avatar of Najam Uddin
Najam Uddin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I recopied and then didn't get the error message, thanks for your help!