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 usesCROSS APPLY
andtable valued constructor
Try with dynamic UNPIVOT:
Use UNPIVOT:
You can also use a normal UNPIVOT for this.
A example using table variables:
Returns: