简体   繁体   中英

SQL Server job run by batch file using sqlcmd

I have created batch file for trigger SQL job using SQLCMD but we are not able to capturing the last status of job run. Please let me know what code we need to write in batch file.

As per our code we have capture only when job is run but we want status of job is failed or pass. I have tried so many thing even able to capture the log but we want only last status of job.

Below is the code using in batch file:

echo execute job
sqlcmd -S "servername" -Q "execute msd.dbo.sp_start_job @job_name='Test_Job_Autosys'" -o c:\sqlscript\output.txt

if errorlevel 1 exit/b

echo job execution completed

Output:

Job started successfully

We are not able to capture job is fail or pass; please let us know how can be capture the status of job.

Like Nick.McDermaid said. the job is started asynchronously, so it returns without waiting to find out what the status of the job is.

I would suggest you wrap the call to msd.dbo.sp_start_job in a stored procedure. That way you can monitor the job status and return or raise an error if the job failed.

There's an article here that explains it in more detail.

Querying agent job status, executing and waiting for job completion from within T-SQL

Hope that helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM