I have a table with many columns like:
Table A:
ColumnA | ColumnB | ColumnC | ColumnD | ColumnE
_______________________________________________
valueA | valueB | valueC | valueD | valueE
How can I insert into Table B like below?
Table B:
ColumnName | ColumnValue
_________________________
ColumnA | valueA
ColumnB | valueB
ColumnC | valueC
ColumnD | valueD
ColumnE | valueE
Thank you!
You need to Unpivot
the data. One option uses CROSS APPLY
and table valued constructor
SELECT ColumnName,
ColumnValue
FROM Yourtable
CROSS APPLY (VALUES ('ColumnA',ColumnA),
('ColumnB',ColumnB),
('ColumnC',ColumnC),
('ColumnD',ColumnD),
('ColumnE',ColumnE))TC(ColumnName, ColumnValue)