I never used loop in a trigger before, I have 3 tables as below:
table 1
1_pk |user |tenant| filter
----------------------------------
| 1 | all | 1 | O
| 2 | john | 1 | AA
| 3 | peter | 1 | AB
| 4 | pat | 1 | AC
| 5 | all | 2 | O
| 6 | tony | 2 | AA
| 7 | jim | 2 | AB
table 2
2_pk |table1-FK|role | tenant
----------------------------------
| 1 | 1 | HR | 1
| 2 | 2 | staff | 1
| 3 | 2 | ceo | 1
| 4 | 5 | staff | 2
table 3
3_pk |table1-FK|table2-FK| tenant
----------------------------------
| 1 | 2 | 1 | 1
| 2 | 3 | 1 | 1
| 3 | 4 | 1 | 1
| 4 | 2 | 2 | 1
| 5 | 2 | 3 | 1
| 3 | 6 | 4 | 2
| 4 | 7 | 4 | 2
I want to create a trigger on table 2 so that
When a record is created on table2 with table1_Fk as either 1 or 5(eg: please see table 2) then the trigger has to insert 3 records(if table1_Fk = 1) or 2 records(if table1_Fk = 5) into table 3. in other words, if filter column(table 1) = 'O' then trigger has to insert X number of rows into table 3 for that particular tenant.
When a record is created on table2 with table1_Fk as either 2,3,4,6 or 7 (eg: please see table 2) then the trigger has to insert only 1 records into table 3.
Is there a way that I can use loop in this trigger as I don't know how many records each tenant has in table 1.
Thanks