Using the following function:
import pyodbc
def execute_side_effect_stmt(sql_stmt: str, params: list):
with get_connection() as conn:
cursor = conn.cursor()
cursor.executemany(sql_stmt, params)
columns = [column[0] for column in cursor.description]
results = cursor.fetchall()
response = []
for row in results:
response.append(dict(zip(columns, row)))
conn.commit()
if not response:
return ''
return response
With the following arguments:
sql = """INSERT INTO dbo.events
(sha, duration)
OUTPUT Inserted.id, Inserted.sha
VALUES (?, ?)"""
params = [('123',1),('456', 2), ('789', 3)]
result = execute_side_effect_stmt(sql, params)
Result only returns the id and sha of the last entry in params. Everything is inserted correctly into the database. Any insights on why only the last insert gives an output would be very welcome.