# Export a dataset to a sql table

**URL:** <https://forum.inductiveautomation.com/t/export-a-dataset-to-a-sql-table/36122>\
**Category:** Ignition\
**Created:** [June 28, 2020, 7:11am UTC](https://forum.inductiveautomation.com/t/export-a-dataset-to-a-sql-table/36122 "2020-06-28T07:11:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![RForshey](https://avatars.discourse-cdn.com/v4/letter/r/dc4da7/32.png) [@RForshey](https://forum.inductiveautomation.com/u/RForshey)\
**Post date:** [June 28, 2020, 7:11am UTC](https://forum.inductiveautomation.com/t/export-a-dataset-to-a-sql-table/36122/1 "2020-06-28T07:11:49Z")

</div>

Hello! I’m trying to export a dataset to a sql table, here is my script:

```auto
TableData = event.source.parent.getComponent('Quote_Review').data
# Convert to PyDataset
Data = system.dataset.toPyDataSet(event.source.parent.getComponent('Quote_Review').data)

# Start
txID = system.db.beginTransaction(timeout=5000)

# Loop through the rows in the dataset, and insert them into the database.
for row in Data:
	column1 = row['Id']
	column2 = row['Quote']
	column3 = row['Top_Level_PN']
	column4 = row['Revision']
	column5 = row['Sold_TO']

sql = """
	INSERT INTO [Quotes].dbo.[Quote_Test]
		(Id, Quote, Top_Level_PN, Revision, Sold_TO)
	VALUES
		(?, ?, ?, ?, ?)
"""
system.db.runPrepUpdate(sql, [column1, column2, column3, column4, column5], tx=txID)
	
# Commit and close
system.db.commitTransaction(txID)
system.db.closeTransaction(txID)

```

The script works but it doesn’t iterate. The dataset I’m exporting has 5 rows (as an example) but the script only causes the last row to populate in the sql table. Any ideas?

---

<div class="post-metadata">

**Author:** ![KGarner](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kgarner/32/15999_2.png) [@KGarner](https://forum.inductiveautomation.com/u/KGarner)\
**Post date:** [June 28, 2020, 2:11pm UTC](https://forum.inductiveautomation.com/t/export-a-dataset-to-a-sql-table/36122/2 "2020-06-28T14:11:44Z")

</div>

This looks like a simple indentation issue.

Based on your code, the only actions that will iterate are those that set the column variables because those are the only lines indented after the for loop declaration. If you indent the lines that specify and run the query, that should fix your issue.

```auto

TableData = event.source.parent.getComponent('Quote_Review').data
# Convert to PyDataset
Data = system.dataset.toPyDataSet(event.source.parent.getComponent('Quote_Review').data)

# Start
txID = system.db.beginTransaction(timeout=5000)

# Loop through the rows in the dataset, and insert them into the database.
for row in Data:
	column1 = row['Id']
	column2 = row['Quote']
	column3 = row['Top_Level_PN']
	column4 = row['Revision']
	column5 = row['Sold_TO']

    sql = """
	INSERT INTO [Quotes].dbo.[Quote_Test]
		(Id, Quote, Top_Level_PN, Revision, Sold_TO)
	VALUES
		(?, ?, ?, ?, ?)
    """
    system.db.runPrepUpdate(sql, [column1, column2, column3, column4, column5], tx=txID)
	
# Commit and close
system.db.commitTransaction(txID)
system.db.closeTransaction(txID)

```

---

<div class="post-metadata">

**Author:** ![RForshey](https://avatars.discourse-cdn.com/v4/letter/r/dc4da7/32.png) [@RForshey](https://forum.inductiveautomation.com/u/RForshey)\
**Post date:** [June 28, 2020, 4:53pm UTC](https://forum.inductiveautomation.com/t/export-a-dataset-to-a-sql-table/36122/3 "2020-06-28T16:53:02Z")

</div>

You are right! Thanks so much for the help, I really appreciate it.
