【发布时间】:2016-06-14 10:49:34
【问题描述】:
我正在尝试检索正在运行的结果表单
import pymssql
conn = pymssql.connect(server='IP', user='domain\user', password='PSWD', tds_version='8.0')
cursor = conn.cursor()
cursor.execute("EXEC msdb.dbo.sp_start_job @job_name = 'jobname'")
当它将作业添加到提示以进行处理时,它不会返回任何内容,但是当作业未运行时,它会返回一些东西,例如用于测试的情况
Traceback (most recent call last):
File "shared/python3", line 85, in <module>
cursor.execute("EXEC msdb.dbo.sp_start_job @job_name = ''")
File "pymssql.pyx", line 467, in pymssql.Cursor.execute (pymssql.c:7533)
pymssql.OperationalError: (14262, "The specified @job_name ('') does not exist.DB-Lib error message 14262, severity 16:\nGeneral SQL Server error: Check messages from the SQL Server\n")
在这种情况下,错误指出 Job_name 不存在。我想要做的是将结果放在一个字符串变量上,我可以解析它以进行错误控制......
我试过这个:
import sys
# Store the reference, in case you want to show things again in standard output
old_stdout = sys.stdout
# This variable will store everything that is sent to the standard output
result = StringIO()
sys.stdout = result
# Here we can call anything we like, like external modules, and everything that they will send to standard output will be stored on "result"
cursor.execute("EXEC msdb.dbo.sp_start_job @job_name = 'jobname'")
# Redirect again the std output to screen
sys.stdout = old_stdout
# Then, get the stdout like a string and process it!
result_string = result.getvalue()
process_string(result_string)
link。但无法让它工作。
【问题讨论】:
-
试试 sys.stderr 而不是 sys.stdout
-
@peterdemin 我试过没有成功,像这样...
old_stdout = sys.stderr result = StringIO() sys.stderr = result cursor.execute("EXEC msdb.dbo.sp_start_job @job_name = 'jobname'") sys.stderr = old_stdout result_string = result.getvalue() print result_string
标签: python linux execute pymssql