I want to run a query looping through rows, but how do I reference the current loop row field in another query?

I have query1 that runs, and let's say it returns 10 rows back and 5 columns, called column1, column2, column3, etc.

What I'm trying to accomplish, is using Javascript I assume, and to have it execute query2 on each of the 10 rows from query1. That part is fine and not too difficult.

Where I'm suck though, is in query2, how do I reference current loop row in the query?

Keeping it simple, let's say query2 is a basic insert query:

INSERT INTO table1 (column1) VALUES ("******");

Where I'm stuck is the ****** part, as I basically want it to be:

INSERT INTO table1 (column1) VALUES ("CURRENT LOOP ROW column3 VALUE");

Hopefully that makes sense.

Would I create a retool variable, and then have the Javascript function set the variable to the current row's data, and then use that variable in my query, rinse and repeat?

Hi @lmonroe,
Have you look into additionalScope? It allows you to pass data into query2 to be used.

1 Like

Thank you! I hadn't seen this yet so this looks to be exactly what I need!