make python wait for stored procedure to finish executing

后端 未结 4 1441
轻奢々
轻奢々 2020-12-29 10:33

I have a python script that uses pyodbc to call an MSSQL stored procedure, like so:

cursor.execute(\"exec MyProcedure @param1 = \'\" + myparam + \"\'\")
         


        
4条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-29 11:14

    i think my way is alittle bit more crude but in the same time much more easy to understand:

    cursor = connection.cursor()
        SQLCommand = ("IF EXISTS(SELECT 1 FROM msdb.dbo.sysjobs J JOIN 
    msdb.dbo.sysjobactivity A ON A.job_id = J.job_id WHERE J.name ='dbo.SPNAME' AND 
    A.run_requested_date IS NOT NULL AND A.stop_execution_date IS NULL) select 'The job is 
    running!' ELSE select 'The job is not running.'")
        cursor.execute(SQLCommand)
        results = cursor.fetchone()
        sresult= str(results)
        while "The job is not running" in sresult:
            time.sleep(1)
            cursor.execute(SQLCommand)
            results = cursor.fetchone()
            sresult= str(results)
    

    while "SPNAME" return "the job is not running" from the jobactivity table sleep 1 second and check the result again. this work for sql job, for SP should like in another table

提交回复
热议问题