# Looping through DB rows

**URL:** <https://community.n8n.io/t/looping-through-db-rows/59364>\
**Category:** Questions\
**Tags:** workflow-building\
**Created:** [October 22, 2024, 9:31am UTC](https://community.n8n.io/t/looping-through-db-rows/59364 "2024-10-22T09:31:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lucifer](https://avatars.discourse-cdn.com/v4/letter/l/ad7895/32.png) [@Lucifer](https://community.n8n.io/u/Lucifer)\
**Post date:** [October 22, 2024, 9:31am UTC](https://community.n8n.io/t/looping-through-db-rows/59364/1 "2024-10-22T09:31:51Z")

</div>

Hello friends,

I am thinking of making a workflow for Google Docs for each value in DB. Let’s clear this up with an example. We have something like this in our DB.

DB data:  
merchant\_id, goods, actual\_sell, expected\_sell  
1, bread, 34, 100  
1, oil, 54, 100  
1, chair, 12, 100  
2, medicine, 40, 100  
2, towel, 23, 100  
2, fruits, 89, 100

we have defined templated with variable name inside it as per the different merchant\_id. Now, we want to make a report of selling based on merchant\_id. If merchant\_id = 1 then grap the details of it and also DB\_2 contains the further related details of merchant\_id 1.

If merchant\_id = 2 then grap the details of it and also DB\_3 contains the further related details of merchant\_id 2 and so on.

So, now, can I create the workflow that will go through each row in DB? If the merchant ID comes first, then copy the Google Doc with that merchant ID that contains the variable and insert the details of DB, DB\_2, DB\_3, etc. based on variable name defile in google doc node.

Final result will be 1 google doc for each merchant\_id with related details.

My current workflow without loop. (With this I need this flow for each merchant\_id which is too much )

 ![image](https://europe1.discourse-cdn.com/n8n/original/3X/2/f/2fe20c962ee7566bc31f2af86a50c548445452bd.png)

---

<div class="post-metadata">

**Author:** ![n8n](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/n8n/32/196425_2.png) [@n8n](https://community.n8n.io/u/n8n)\
**Post date:** [October 22, 2024, 9:31am UTC](https://community.n8n.io/t/looping-through-db-rows/59364/2 "2024-10-22T09:31:53Z")

</div>

It looks like your topic is missing some important information. Could you provide the following if applicable.

- **n8n version:**
- **Database (default: SQLite):**
- **n8n EXECUTIONS\_PROCESS setting (default: own, main):**
- **Running n8n via (Docker, npm, n8n cloud, desktop app):**
- **Operating system:**

---

<div class="post-metadata">

**Author:** ![aya](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/aya/32/194036_2.png) [@aya](https://community.n8n.io/u/aya)\
**Post date:** [October 24, 2024, 12:31pm UTC](https://community.n8n.io/t/looping-through-db-rows/59364/3 "2024-10-24T12:31:58Z")

</div>

Hi @Lucifer ,

Not sure if I’ve fully understood your question, but have you considered joining the different databases so that you have a new database, each row containing all the information needed to create a report per goods with a specific merchant\_id including details from other databases? Then, you can simply generate a Google doc for each row of that new db?

> **Tip for sharing your workflow in the forum**
>
> #### Pasting your n8n workflow
> 
> * * *
> 
> Ensure to copy your n8n workflow and paste it in the _code block_, that is in between the pairs of triple backticks, which also could be achieved by clicking `</>` (preformatted text) in the editor and pasting in your workflow.
> 
> ```  
> \<your workflow\>  
> ```
> 
> Make sure that you’ve removed any sensitive information from your workflow and include dummy data or pinned data as much as you can!
> 
> * * *

---

<div class="post-metadata">

**Author:** ![Lucifer](https://avatars.discourse-cdn.com/v4/letter/l/ad7895/32.png) [@Lucifer](https://community.n8n.io/u/Lucifer)\
**Post date:** [October 24, 2024, 1:38pm UTC](https://community.n8n.io/t/looping-through-db-rows/59364/4 "2024-10-24T13:38:51Z")

</div>

Hey @aya,

The final report contain multiple data that needs to grep from the different DB with different schema (Indoor items, Outdoor items and Both). Now, for each merchant\_id, we need 1 separate report. Currently, we are multiplying the snapshot architecture for x number of merchants. If we can add condition like if merchant\_id is 1 then take the related merchant\_id template (with variable), copy it, run query\_1(Indoor item), query\_2(Outdoor item), query\_3(Both) and place it on google doc in the place of variable that define in template related to merchant = 1 report. Same goes to merchant 2, 3, 4 etc.

If merchant\_id changes; templates with variable changes, query\_1/2 and query\_3 change.

I hope I can explain in a better way. Let me know, if you are still in any doubt.

Thanks

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/system/32/195824_2.png) [@system](https://community.n8n.io/u/system)\
**Post date:** [January 22, 2025, 1:39pm UTC](https://community.n8n.io/t/looping-through-db-rows/59364/5 "2025-01-22T13:39:32Z")

</div>

Closed on January 22, 2025, 1:39pm UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
