简体   繁体   中英

No Results from pyodbc using table type

I'm creating a #Temp table within this connection, then trying to convert that to a user defined table type and pass it to a stored procedure. this works fine in Microsoft SQL Server Management Studio, but i can't get it to work in python using pyodbc.

SET NOCOUNT ON;
USE TestDB
DECLARE @Temp as dbo.TempType;
INSERT INTO @Temp SELECT * FROM #Temp;
SELECT * FROM @Temp;

(In the actual code, the select statement at the end will be the stored procedure but in testing I'm trying to get this to work). Trying to do fetchall on the cursor returns:

Traceback (most recent call last):
  File "C:\Anaconda\lib\site-packages\IPython\core\interactiveshell.py", line 2883, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-237-dfc0750240b3>", line 1, in <module>
    dt = cur.fetchall()
ProgrammingError: No results.  Previous SQL was not a query.

Within the same connection I can SELECT * FROM #Temp just fine -- but converting to table type doesn't seem to allow me to select. Any ideas ?

Not sure if this helps, but it solved my problem when using iPython notebooks and pyodbc with #temp tables:

import pyodbc
con = pyodbc.connect('Driver={SQL Server Native Client 10.0};Server=somedatabaseserver.com;Database=SomeDB;Trusted_Connection=yes;')
cur = con.cursor()

query1 = """SELECT * into #temp FROM [SomeDB].[SomeTable]"""
query2 = """SELECT * FROM #temp"""

cur.execute(query1)
cur.commit()
cur.execute(query2).fetchall()

The trick here is to commit the into #temp query and then execute work against it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM