I have been trying to find sample code with using SQL Server 2005, I need to start an Agent Job and wait until it finishes process and then get the success or failure.
I know that I can use
EXEC MSDB.dbo.sp_start_job @Job_Name = 'JobName'
to start the job but I can't find any sample code to poll the msdb.dbo.sp_help_job
to know when it is completed and then find out if it was successful or failed.
You can consult the run_status column in the sysjobhistory table. 0 indicates a failure.
Here is code that I wrote for this purpose. One caveat is that it does not handle the case where the job is already running when this procedure is executed.
Maybe not an awfully reliable method, but I might try to have the job write to a certain table both at the beginning and at the end of the process, and to poll that table in my client application (or to use ADO events to trigger corresponding event handlers).