我试图检索运行
的结果表单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'")
当它将作业添加到cue进行处理时它不会返回任何内容,但是当作业没有运行时它会返回类似于测试的内容
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。但无法让它发挥作用。
答案 0 :(得分:2)
您正在查看回溯,因为您没有处理作业名称不存在时发生的异常。如果要捕获错误消息,只需捕获异常即可。作为一个通用的例子,而不仅仅是做
crsr.execute(sql)
你可以做到
try:
crsr.execute(sql)
except Exception as e:
(error_code, error_message) = e
然后使用error_code
和error_message
值将它们写入日志文件,或将它们吐出到控制台或其他任何内容。