90abyss
90abyss

Reputation: 7347

Create SQL Server temporary tables inside Python script

I'm using pypyodbc to connect to the SQL Server. I want to store my resultset into a temporary table like we do in SQL. But everytime I try to do it - I get this error message:

pypyodbc.ProgrammingError: ('24000', '[24000] [Microsoft][ODBC SQL Server Driver]Invalid cursor state')

This is what I'm trying to query:

querytest = "SELECT id into #temp from Team"
cursor1.execute(querytest);
var = cursor1.fetchall()
print(var[:10])

Upvotes: 1

Views: 8108

Answers (1)

Gord Thompson
Gord Thompson

Reputation: 123654

The query

SELECT id into #temp from Team

does not return a result set; it just creates the #temp table. You now need to SELECT from the #temp table in order to see the results, i.e., something like

querytest = "SELECT id into #temp from Team"
cursor1.execute(querytest);  # no result set returned
cursor1.execute("SELECT id FROM #temp")  # result set returned here
var = cursor1.fetchall()
print(var[:10])

Upvotes: 6

Related Questions