# Execute sql insert statements via sql alchemy within for loop

**URL:** <https://www.singlestore.com/forum/t/execute-sql-insert-statements-via-sql-alchemy-within-for-loop/4848>\
**Category:** Procedural SQL\
**Created:** [August 8, 2022, 5:40am UTC](https://www.singlestore.com/forum/t/execute-sql-insert-statements-via-sql-alchemy-within-for-loop/4848 "2022-08-08T05:40:05Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![smartdnshail1](https://www.singlestore.com/forum/letter_avatar_proxy/v4/letter/s/a698b9/32.png) [@smartdnshail1](https://www.singlestore.com/forum/u/smartdnshail1)\
**Post date:** [August 8, 2022, 5:40am UTC](https://www.singlestore.com/forum/t/execute-sql-insert-statements-via-sql-alchemy-within-for-loop/4848/1 "2022-08-08T05:40:05Z")

</div>

I need to extract a bunch of records within a singlestore database and insert the records into another table. For performance, the ideal way to do this is to create a query string with an Insert Into statement and iterate through on a daily basis.

I can’t seem to get python to execute the query in the database, but it appears to run successfully?

```auto
fh = 'file_containing_insert_select_query.sql'
qry = open(fh).read()
for i in range(2):
    qry_new = some_custom_function_to_replace_dates(qry, i)
    engine = tls.custom_engine_function()
    engine.execute(qry_new)

```

I’ve verified that the sql statements created by my custom function can be copy/pasted to a sql editor and executed successfully, but it won’t run in python… any thoughts?

---

<div class="post-metadata">

**Author:** ![hanson](https://www.singlestore.com/forum/user_avatar/www.singlestore.com/hanson/32/196_2.png) [@hanson](https://www.singlestore.com/forum/u/hanson)\
**Post date:** [August 8, 2022, 10:12pm UTC](https://www.singlestore.com/forum/t/execute-sql-insert-statements-via-sql-alchemy-within-for-loop/4848/2 "2022-08-08T22:12:59Z")

</div>

It’s not clear what the issue is; would need to see the error message to have more to go on. But if you want it to run really fast, it’s usually better to use set-oriented SQL, like INSERT INTO targetTbl … SELECT FROM …;

That way, it doesn’t have to round-trip through the client app.
