mkleehammer / pyodbc

Python ODBC bridge
https://github.com/mkleehammer/pyodbc/wiki
MIT No Attribution
2.88k stars 561 forks source link

Unable to obtain INFO messages using fast_executemany from SQL Server connection #1299

Closed BobDorr closed 4 months ago

BobDorr commented 8 months ago

Issue

Attempting to retrieve the message when using executemany. Using a single execution, I am able to output the messages, but I have not determined how to invoke the SQLGetDiagRec when using executemany.

The sample procedure shows the INFO messages when I also do a raiserror but if the raiserror is not present I am unable to output the print message(s).

Repro

''' use tempdb go

drop table if exists t go drop procedure if exists spTest go

create table t ( id int, id2 int ) go

create procedure spTest @id int, @id2 int as begin insert into t values (@id, @id2) print 'In the procedure' -- raiserror('Bob', 16, 1) -- If I add this then I get all the print messages end go

'''

import pyodbc

connection_string = ''' DRIVER={ODBC Driver 17 for SQL Server}; SERVER=localhost; DATABASE=tempdb; UID=sa; PWD=xxxxxxxxxxx; '''

Creates the connection

conn = pyodbc.connect(connection_string) conn.autocommit = False

cursor = conn.cursor() params = [(i,i) for i in range(10)] cursor.fast_executemany = True

cursor.executemany("{call spTest(?, ?)}", params)

This works but the ExecuteMany I am unable to get the calls to SQLGetDiagRec for messages

cursor.execute("{call spTest(1, 2)}")

print(cursor.rowcount) print(cursor.messages)

while(cursor.nextset()): print(cursor.messages)

cursor.commit() # Does not show any information message here either but this can have them cursor.close()

gordthompson commented 8 months ago

Possibly related: #1245

You might try installing from this branch to see if it helps.

pip install git+https://github.com/keitherskine/pyodbc@issue1238