Looking for best advice on how to do this:
I have an insert like this:
insert into empty_table (
column_1,
column_2,
column_3,
column_4
)
(select
sequence_1.nextval,
v_variable_1,
v_variable_2,
value_1
from template_table
where some_value = "value 1");
The select statement on its own returns 22 records. What I want to do is iterate over this insert and set the variables equal to values from another query like:
select
variable_1,
variable_2
from table_with_var_values
where some_other_value = "value 2";
This query returns about 180 records and looks like this:
variable_1 variable_2
------------------------
Abc 101
Def 102
Ghi 103
Jkl 104
etc...
So the ultimate result will be that empty_table holds 3,960 records (22 x 180) and will look something like this:
column_1 column_2 column_3 column_4
--------------------------------------------
1 Abc 101 Spring
2 Def 102 Summer
3 Ghi 103 Spring
4 Jkl 104 Fall
etc...
I can store this as function in a package but I am unsure how to begin structuring a function like this. Thanks in advance for any help.
Why not just combine the two queries and do your INSERT, as in:
Because there’s no join criteria between TEMPLATE_TABLE and TABLE_WITH_VAR_VALUES you should get a Cartesian join, where every row of both tables are joined together, which is what I think you want. No need to loop to get that.
Not sure which of your template_table columns had the ‘Spring’, ‘Summer’, and ‘Fall’ values so I just picked v_variable_1 – substitute as needed.
Share and enjoy.