I have a table which contains a list of dynamic SQL views that needs to be created
SEEDING_TABLE
-------------
KEYVALUE|VIEW_TO_BE_CREATED|FROMTABLE|NOOFCOLS
1|A|A1|3
2|B|B1|4
3|C|C1|5
The other table which contains the actual column names for the above seeding table
ORDERCOLS_FORVIEW
KEYVALUE|FROMTABLE|COLSAVAILABLE
1|A1|NUM1
1|A1|NUM2
1|A1|NUM3
2|B1|NUM1
2|B1|NUM2
2|B1|NUM3
2|B1|NUM4
3|C1|NUM1
3|C1|NUM2
3|C1|NUM3
3|C1|NUM4
3|C1|NUM5
Definition of the table FROMTABLEs as follows
A1 -> KEYVALUE|NUM1|NUM2|NUM3
B1 -> KEYVALUE|NUM1|NUM2|NUM3|NUM4
C1 -> KEYVALUE|NUM1|NUM2|NUM3|NUM4|NUM5
After doing all our logic and magic the outcome should be a dynamic SQL which should produce the below view statements
DYNAMIC_ENTRIES -> TEXT|TABLE|RANK
TEXT |TABLE | RANK
CREATE OR REPLACE VIEW A AS SELECT | A | 1
KEYVALUE, | A | 2
NUM1 AS KEY1, | A | 3
NUM1 AS NO1, | A | 4
NUM1||'|'||NUM2 AS KEY2, | A | 5
NUM2 AS NO2, | A | 6
NUM1||'|'||NUM2||'|'||NUM3 AS KEY3, | A | 7
NUM3 AS NO3 | A | 8
FROM A1; | A | 9
CREATE OR REPLACE VIEW B AS SELECT | B | 1
KEYVALUE, | B | 2
NUM1 AS KEY1, | B | 3
NUM1 AS NO1, | B | 4
NUM1||'|'||NUM2 AS KEY2, | B | 5
NUM2 AS NO2, | B | 6
NUM1||'|'||NUM2||'|'||NUM3 AS KEY3, | B | 7
NUM3 AS NO3, | B | 8
NUM1||'|'||NUM2||'|'||NUM3||'|'||NUM4 AS KEY4, | B | 9
NUM4 AS NO4 | B | 10
FROM B1; | B | 11
CREATE OR REPLACE VIEW C AS SELECT | C | 1
KEYVALUE, | C | 2
NUM1 AS KEY1, | C | 3
NUM1 AS NO1, | C | 4
NUM1||'|'||NUM2 AS KEY2, | C | 5
NUM2 AS NO2, | C | 6
NUM1||'|'||NUM2||'|'||NUM3 AS KEY3, | C | 7
NUM3 AS NO3, | C | 8
NUM1||'|'||NUM2||'|'||NUM3||'|'||NUM4 AS KEY4, | C | 9
NUM4 AS NO4 | C | 10
NUM1||'|'||NUM2||'|'||NUM3||'|'||NUM4||'|'||NUM5 AS KEY5, | C | 11
NUM5 AS NO5 | C | 12
FROM C1; | C | 13
Assuming for every entry in the seeding table, we have the complete list of columns available in the lookup. The logic is that for every entry in the seeding table, we need to insert entries into the final dynamic SQL table to create views for the entries in VIEW_TO_BE_CREATED column by using the FROMTABLE. For every column in the FROMTABLE that has the sequence like NUM1...NUMn they need to concatenated like a tree.
I am confused on how to approach this. We can create any number of intermediate tables or views to achieve this. Any pointers for this would be much appreciated?