使用pyodbc调用MSSQL存储过程时的结果不一致

时间:2015-04-13 22:55:42

标签: python sql sql-server stored-procedures pyodbc

我在MSSQL服务器中有以下SP:

ALTER Procedure [dbo].[pQMx_OBFL_GetFileByStatus]

@status varchar(100) 

as

Begin
SET NOCOUNT ON;

select top 200  Case when P3.Pos - P2.Pos - 1<0 then NULL Else replace(substring(File_Name, P2.Pos + 1, P3.Pos - P2.Pos - 1),'=','') END as PID,  File_ID, File_Name, '/data1/obfl/OBFL/'+C.siteName AS remoteFromDir into #temp
from qmx_obfl_log with (nolock)
  cross apply (select (charindex('_', File_Name))) as P1(Pos)
  cross apply (select (charindex('_', File_Name, P1.Pos+1))) as P2(Pos)
  cross apply (select (charindex('_', File_Name, P2.Pos+1))) as P3(Pos)
   INNER JOIN fileTransfer.Config C with (nolock) ON Location_ID = C.ID
WHERE qmx_obfl_log.Status = @status 

select top 1 File_ID, File_Name, remoteFromDir into #temp1 from #temp with (nolock) where PID in
(select distinct PID from QMx.DBO.qmx_SN_Master with (nolock)
     where PF in (Select Distinct ProductFamily from QMx.DBO.LCC_PF with (nolock) where OBFL_Parser_YN = 'Y'))

UPdate Qmx_obfl_log set Status='Parsing' where File_ID in  (Select File_ID from #temp1)

Select * from #temp1

End

我使用以下python代码从SP获取结果:

import pyodbc
conn = pyodbc.connect("DRIVER={FreeTDS}; SERVER=*xxx*; PORT=51005; DATABASE=*xxx*; UID=*xxx*; PWD=*xxx*; TDS_Version=8.0")
conn.timeout = 20
cursor = conn.cursor()
select_sql = "exec pQMx_OBFL_GetFileByStatus 'Transferred'"
rows = cursor.execute(select_sql).fetchall()
cursor.commit()
rows

我看到的问题是,有时行会传递到空列表,有时会获得预期的结果。这只发生在pyodbc上,但如果我直接从MS Studio调用SP则不行。为什么会出现这种不一致?感谢。

0 个答案:

没有答案