我有一个Python脚本,它使用pyodbc库来运行一些查询,我有任务调度程序在设定的时间表上运行。该脚本上周一直没有问题,但它突然遇到了错误:
DatabaseError: Execution failed on sql 'select * from #output ('42S02', "[42S02] [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Invalid object name '#output'. (208) (SQLExecDirectW)")
当我尝试使用pd.read_sql
从查询中提取数据时,会出现此错误。非常奇怪的是,根本没有对脚本进行任何修改。直到今天它一直很好。
我研究了stackoverflow上的类似问题并尝试使用相同的解决方案(创建全局临时表而不是本地临时表)并且问题仍然存在。奇怪的是,查询包括两个临时表#data
和#output
,我可以使用#data
访问pd.read_sql
的内容,没有任何问题,但无论出于何种原因,尝试在另一个临时表上使用相同的方法会导致上面的错误。
这里的代码本身可能有点长,但这大部分都是类似的(如果不是问题,我很乐意分享整个事情):
connection = pyodbc.connect(...)
cursor = connection.cursor()
query = """create table #data (...)
... #some other commands here in the middle
create table #output (...)
... #two more lines of SQL commands and that's it
"""
cursor.execute(query)
totals = pd.read_sql("""select * from #output"""), connection)
行cursor.execute(query)
似乎没有产生错误,因为如果我只跑到该行,我会<pyodbc.Cursor at 0xef2eea0>
。
我很感激你的帮助。
编辑:我怀疑错误消息是由于create table #output
语句之前有一个while循环部分,并且它以某种方式阻止下一个命令正常执行。我使用上面的示例代码包含了更多细节:
connection = pyodbc.connect(...)
cursor = connection.cursor()
query = """create table #data (...)
... #some other commands here in the middle
declare @order int, @limit int, @check varchar(20)
set @order = 1
set @limit = (select count(*) from #data where [weekday] = 'Friday')
while @order <= @limit
begin
update #data
set [order] = @order
where #data.[date] = (select top 1 [date] from #data where [order] is null order by [date] asc)
if (select top 1 [weekday] from #data where [order] is not null order by [date] desc) = 'Friday'
set @order = @order + 1
update #data
set weekly_desc = (select top 1 weekly_desc from #data where [weekday] = 'Friday' and [order] is null order by [date] asc)
where [order] = @order
end
create table #output (...)
"""
cursor.execute(query)
totals = pd.read_sql("""select * from #output""", connection)
答案 0 :(得分:0)
经过多次挖掘后,我发现在更新#data
表(#create table #output...
之前的步骤后,如果我让Python执行pd.read_sql("""select * from #data""", connection)
,控制台显示最后几行#data
列中的NaN
包含order
值。我不知道为什么会因为在SQL Server中运行查询而没有问题。这可能解释了为什么它无法解决继续进行下一步create table #output
。
我再次使用它的唯一方法是重写部分查询,特别是while循环部分,从
更改它while @order <= @limit
begin
update #data
set [order] = @order
where #data.[date] = (select top 1 [date] from #data where [order] is null order by [date] asc)
if (select top 1 [weekday] from #data where [order] is not null order by [date] desc) = 'Friday'
set @order = @order + 1
update #data
set weekly_desc = (select top 1 weekly_desc from #data where [weekday] = 'Friday' and [order] is null order by [date] asc)
where [order] = @order
end
到
while @order <= @limit
begin
update #data
set [order] = @order
where #data.[date] = (select top 1 [date] from #data where [order] is null order by [date] asc)
if (select top 1 [weekday] from #data where [order] is not null order by [date] desc) = 'Friday'
set @order = @order + 1
end
update #data
set weekly_desc = t.weekly_desc
from (select [order], weekly_desc
from #data
where weekly_desc is not null
) as t
where #data.weekly_desc is null
and #data.[order] = t.[order]
create table #output
步骤和其他命令之后,我能够让整个脚本运行没有问题。希望它不会神秘地再次停止工作。
谢谢大家的帮助!