Create SQL Server temporary tables inside Python script

非 Y 不嫁゛ 提交于 2019-12-11 10:14:52

问题


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])

回答1:


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])


来源:https://stackoverflow.com/questions/32683378/create-sql-server-temporary-tables-inside-python-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!