Issue 243: Exception "No results. Previous SQL was not a query." when stored SQL 2000 proc uses print statement
Status:  Complete
Owner: ----
Closed:  Mar 2012
Reported by gol...@gmail.com, Mar 5, 2012
What steps will reproduce the problem?
1. Create a SQL 2000 stored procedure:
CREATE PROCEDURE p_ConfusePyodbc AS
print 'hello world'
SELECT 1 AS Data
GO

2. Execute the procedure with pydobc and access the records in the cursor.
import pyodbc
conn = pyodbc.connect("<<connection string>>")
cursor = conn.cursor()
cursor.execute("exec p_ConfusePyodbc")
print cursor.fetchone()

3. If the 'hello world' print statement executes, pyodbc throws the exception "No results. Previous SQL was not a query." is thrown.

3a. Removing the print statement from the proc makes the problem go away.


The expected output is a recordset. Instead, I see an exception.


What version of the product are you using? On what operating system?
3.0.3 on Python 2.7.

Please provide any additional information below.
We use print statements in SQL 2000 all the time for debugging purposes. Having a developer who doesn't know anything about pyodbc add a print statement, and thereby take down any app that uses pydbc is pretty dangerous!
Mar 5, 2012
Project Member #1 mkleehammer
This was fixed in 3.0.5, which I don't have many installers for.  I do have one for 32-bit Windows Python 2.7, however, which it looks like you are using.  Thanks.
Status: Complete
Mar 5, 2012
#2 gol...@gmail.com
Ah, cool! Thanks! I'll try it out.
Jun 7, 2012
#3 james.qu...@gmail.com
Using 3.0.5 (pyodbc-3.0.5-py2.7-win32.egg), I still see the same issue. I call a stored procedure that sends out results using print statements, but a call to fetchal() or fetchone() fails. The nextset() function returns True if I call it 9 times, which corresponds to the number of lines of output, but I cannot get to the output itself. Any thoughts?
Dec 20, 2013
#4 joshseg...@google.com
Using the solution from http://stackoverflow.com/questions/7753830/mssql2008-pyodbc-previous-sql-was-not-a-query worked for me. Issue "SET NOCOUNT ON;" before your exec statement. This should really be fixed by the library, but as a workaround it suffices.