在Python脚本中创建SQL Server临时表 [英] Create SQL Server temporary tables inside Python script
问题描述
我正在使用pypyodbc连接到SQL Server.我想将结果集像在SQL中一样存储到临时表中.但是每次我尝试执行此操作时,我都会收到以下错误消息:
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 服务器驱动程序]无效的游标状态')
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])
推荐答案
查询
SELECT id into #temp from Team
不返回结果集;它只是创建#temp
表.现在,您需要从#temp
表中进行SELECT,以查看结果,即类似
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])
这篇关于在Python脚本中创建SQL Server临时表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!