I have a table which looks like this:
table1
column1 column2 column3
1 2 3
a 6 9
b 8 7
I need to copy this data to another table in this format:
table2
column_name value
column1 1
column1 a
column1 b
column2 2
column2 6
column2 8
column3 3
column3 9
column3 7
There are more than 100 tables like table1 with millions of rows. I need to copy this in table2.
So, I was planning to write like:
INSERT INTO table2
(column_name, value)
SELECT 'column1', column1
FROM table1;
INSERT INTO table2
(column_name, value)
SELECT 'column2', column2
FROM table1;
INSERT INTO table2
(column_name, value)
SELECT 'column3', column3
FROM table1;
I have 80 columns like this. Is there an efficient way of writing this which also works fast in copying data in Oracle?
Edit: changed example dataset to include different type of data. If I use unpivot, it gives me:
ORA-01790: expression must have same datatype as corresponding expression