Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server temp table not available in pyodbc code

I'm running a series of complex sql queries in python and it involves temp tables. My auto-commit method doesn't seem to be working to retrieve the data from the temp table. The code snippet I'm using below and this is the output I'm getting:

testQuery="""
    Select top 10 *
    INTO #Temp1
    FROM Table1 t1
    JOIN Table2 t2
    on t1.key=t2.key
"""
    cnxn=pyodbc.connect(r'DRIVER={SQL Server Native Client 11.0};SERVER=server;DATABASE=DB;UID=UID;PWD=PWD')
    cnxn.autocommit=True
    cursor=cnxn.cursor()
    cursor.execute(testQuery)
    cursor.execute("""Select top 10 * from #Temp1""")
    <pyodbc.Cursor at 0x8f78930>


cnxn=pyodbc.connect(r'DRIVER={SQL Server Native Client 11.0};SERVER=server;DATABASE=DB;UID=UID;PWD=PWD')
cnxn.autocommit=True
cursor=cnxn.cursor()
cursor.execute(testQuery)
cursor.execute("""Select top 10 * from #Temp1""")
like image 224
sudhareg Avatar asked Jun 16 '16 15:06

sudhareg


1 Answers

The SET NOCOUNT ON is what worked for me.

execute Method () - JDBC Driver for SQL Server | Microsoft Docs

Return Value

  • true, if the statement returns a result set.
  • false, if it returns an update count or no result.

If you want a result set, then setting SET NOCOUNT ON s the setting you need in your statements.

like image 84
Donnie Drake Avatar answered Oct 27 '22 21:10

Donnie Drake