0

With python, I am trying to read a table from SQL Server, populate one field and load the resulting table back to SQL Server as a new table. I'm using "petl".

I can read the table with no problems, I populate the field successfully, but then I get an error when I try to load the resulting table back to SQL Server.

This is the python code I'm using:

import petl as etl
import pyodbc

def populate_field(z, row):
    ...

    
    
con = pyodbc.connect('Trusted_Connection=yes', driver='{SQL Server}', server=r'my_server', database='my_db')

query = r'SELECT TOP 10 * FROM my_table'
tab = etl.fromdb(con, query)

tab = etl.convert(tab, 'my_field', populate_field, pass_row=True)

etl.todb(tab, con, 'my_new_table') 

And this is the error message I get on "etl.todb(tab, con, 'my_new_table')"

Error: ('HY000', '[HY000] [Microsoft][ODBC SQL Server Driver]Connection is busy with results for another hstmt (0) (SQLExecDirectW)')
Pitrako Junior
  • 143
  • 1
  • 7
  • Does this answer your question? https://github.com/mkleehammer/pyodbc/issues/300 – medium-dimensional May 11 '22 at 11:25
  • Thanks. I had already seen that link. It's one of the very few talking about that error. Unfortunately it didn't really help. I find very strange that I have even tried to close the connection and cursor, created them again, but it still fails. – Pitrako Junior May 11 '22 at 11:32
  • Thanks for the reply! I don't have much experience with pyodbc, but I'll check if I can find something useful to solve your problem. – medium-dimensional May 11 '22 at 11:35

0 Answers0