# Insert Select Query results into another DB Table

**URL:** <https://forum.inductiveautomation.com/t/insert-select-query-results-into-another-db-table/18470>\
**Category:** Ignition\
**Created:** [June 14, 2018, 3:49pm UTC](https://forum.inductiveautomation.com/t/insert-select-query-results-into-another-db-table/18470 "2018-06-14T15:49:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [June 14, 2018, 3:49pm UTC](https://forum.inductiveautomation.com/t/insert-select-query-results-into-another-db-table/18470/1 "2018-06-14T15:49:59Z")

</div>

I have a named query using parameters to make a select that returns multiple rows, for example:

 ![](https://us1.discourse-cdn.com/inductiveautomation/original/2X/f/f47d89c024f8bf6b61914fd3b41d4af17aa4d20a.JPG)  
I want to insert the results into another table, how do I pass the results one row at a time into the insert query?  
my code so far:

```auto
vars = {"style":style, "size1":size1, "size2":size2, "material1":material1, "material2":material2}

		
results = system.db.runNamedQuery("BOM/WorkOrder",vars)

		
for index,row in enumerate(results):
         system.db.runNamedQuery("BOM/Insert",{})
```

Thanks

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [June 14, 2018, 5:03pm UTC](https://forum.inductiveautomation.com/t/insert-select-query-results-into-another-db-table/18470/2 "2018-06-14T17:03:53Z")

</div>

I didn’t want to do this, but I guess I should turn the query results into a pydataset and loop through it.

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [June 14, 2018, 5:56pm UTC](https://forum.inductiveautomation.com/t/insert-select-query-results-into-another-db-table/18470/3 "2018-06-14T17:56:18Z")

</div>

You were going to have to loop anyway. 🙂

I have no idea how your named queries are set up, but if you are using the same column names from your first query to the second, you may be able to use it to your advantage:

```auto
vars = {"style":style, "size1":size1, "size2":size2, "material1":material1, "material2":material2}
    
results = system.dataset.toPyDataSet(system.db.runNamedQuery("BOM/WorkOrder",vars))

# Get column names from PyDataSet (comes in a s a list)
columnNames = results.getUnderlyingDataset().getColumnNames()

for row in results:
  # Create dictionary using the list of columnNames 
  # and the list of the row values
  rowDict = dict(zip(columnNames, list(row)))

  system.db.runNamedQuery("BOM/Insert", rowDict)

```

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [June 14, 2018, 6:13pm UTC](https://forum.inductiveautomation.com/t/insert-select-query-results-into-another-db-table/18470/4 "2018-06-14T18:13:37Z")

</div>

I figured the loop was the answer. I’ve got it working now, thanks
