我有一个简单的 Python 脚本,我在其中发送对 SQL Server 2008 存储过程的调用,该存储过程又通过 xp_cmdshell 使用 bcp。当我通过管理控制台调用 SP 时,它工作得很好。当我从 python 脚本调用它时,它会挂起,因为执行会阻止用于 bcp 结果的 tempdb。
有没有办法在不使用事务的情况下从 Python 进行调用(因为我认为这个额外的附加组件正在锁定我的挂起调用)。
这是我的脚本:
import pyodbc as p
def CallExportFiles(conn, procName):
sql = "DECLARE @ret int EXEC @ret = [db].[dbo].[" + procName + "] @I_EMAILPROFILE = N\'SQL_AlertProfile\', @I_EMAILALERTS = N\'xxx@yyy.com\', @I_CUSTOMERID = N\'xxx\', @I_ENVIRO = N\'prod\', @I_COAFILENAME = N\'InvoiceGL\', @I_PERSONFILENAME = N\'Person\', @I_DELEGATESFILENAME = N\'Delegates\', @I_VENDORFILENAME = N\'InvoiceVendors\', @I_DIRECTORY = N\'\\\\xxx\\output\\yyy\\\', @I_ARCHIVEDIR = N\'\\\\xxx\\output\\yyy\\ARCHIVE\\\', @I_FAILUREDIR = N\'\\\\xxx\\output\\yyy\\FAILURE\\\' SELECT \'Return Value\' = @ret"
dbCursor = conn.cursor()
##Locks on this next execute call
dbCursor.execute(sql)
dbCursor.commit()
if __name__ == '__main__':
#connectin to the db with SQL Authentification
conn = p.connect(driver = '{SQL Server Native Client 10.0}', server = r'server', database = 'db', uid = 'sa', pwd = 'xxx')
if (conn == False):
print 'Error, did not connect to the database'
else:
CallExportFiles(conn, 'sp_export_files')
conn.close()
在存储过程中调用时锁定的部分在此处执行(注意:此存储过程工作正常,使用 Management Studio 中的相同字符串调用:
SELECT CONVERT(varchar(1),'Z') AS [SORT],
CONVERT(varchar(64),'GLNumber') AS GLNUMBER,
CONVERT(varchar(1000),'GLDescription') AS GLDESCRIPTION,
CONVERT(varchar(4),'Type') AS [TYPE],
CONVERT(varchar(100),'GLStatusFlag') AS GLSTATUSFLAG,
CONVERT(varchar(100),'UDF1') AS UDF1,
CONVERT(varchar(100),'UDF2') AS UDF2,
CONVERT(varchar(100),'UDF3') AS UDF3,
CONVERT(varchar(100),'UDF4') AS UDF4,
CONVERT(varchar(100),'UDF5') AS UDF5
INTO ##BCP_Results1
INSERT INTO ##BCP_Results1([SORT],GLNUMBER,GLDESCRIPTION,[TYPE],GLSTATUSFLAG,UDF1,UDF2,UDF3,UDF4,UDF5)
SELECT 'A',
GLNUMBER,
GLDESCRIPTION,
[TYPE],
GLSTATUSFLAG,
UDF1,
UDF2,
UDF3,
UDF4,
UDF5
FROM dbname.DBO.GLACCOUNTS
set @sqlstring = 'bcp "SELECT GLNUMBER,GLDESCRIPTION,[TYPE],GLSTATUSFLAG,UDF1,UDF2,UDF3,UDF4,UDF5 FROM ##BCP_Results1 ORDER BY [SORT] DESC " queryout '+@I_DIRECTORY+@I_COAFILENAME+'-'+@I_ENVIRO+'-'+@I_CUSTOMERID+'-'+CONVERT(CHAR(10),GETDATE(),23)+'-'+ LEFT(CONVERT(CHAR(5),GETDATE(),114),2)+'-'+ RIGHT(CONVERT(CHAR(5),GETDATE(),114),2)+'.txt -c -t^| -U sa -P xxxx -S xserverx\xdbx'
EXECUTE master.dbo.xp_cmdshell @sqlstring
drop table ##BCP_Results1