I am trying to build queries to take data from table_1:
| id | path | curr_value |
|---|---|---|
| 1 | name | Company ABC |
| 2 | address | 123 Main St |
| 3 | name | Company 123 |
And use it as the value AND column header for table_2 in SQL.
Please note, table_2 is already created and has the correct column names. It is just empty to begin and the result is the following.
| id | name | address | state |
|---|---|---|---|
| 1 | Company ABC | null | null |
| 2 | null | 123 Main St | null |
| 3 | Company 123 | null | null |
I understand this may look silly, but there's a reason for this, as it is just a piece of the puzzle I am trying to solve.
I have tried something like this:
WITH dynamic_columns AS ( SELECT path, curr_value FROM table_1 ) and
SELECT CONCAT('INSERT INTO \'', path, '\' VALUES (\'', curr_value, '\')') FROM dynamic_columns I get the following:
INSERT INTO 'name' VALUES 'COMPANY ABC' INSERT INTO 'address' VALUES '123 Main St' INSERT INTO 'name' VALUES 'COMPANY 123' I know this isn't correct, but how do I fix it and actually execute?
('INSERT INTO ', path, ' VALUES...)), you'll build proper SQL, but how are you going to execute it? I think you're going to need a programming language."name"for a table. ('name'is a string literal.)