Determining Result of a job started with sp_start_job

  • I have an SSIS package that I have added as a job in SQL Server 2005. The SSIS package runs a query and creates a csv with the results. The plan is to have the end user be able to execute the SSIS package through an ASP.Net 2.0 application and create the csv they need whenever they need to.

    I have a stored proc that starts the job using sp_start_job.

    Everything works just fine, however, If something goes wrong I need to be able to tell the end user. What is the best way to determine the result of a job you started through the sp_start_job?

  • Inside the SSIS package you can make use of "On failure" flow to show the users the error

  • Thanks Vidhya but, If I am starting the job with sp_start_job I don't see how he "on error" in the SSIS package will help me.

    I figure I can query the sysjobhistory table to determine the last execution status for that job, however, this seems like a problem that lots of you SQL Experts would have run into in the past and I was wondering if there was a cleaner/ better way to do this that I am not thinking of.

    Thanks

  • private void exec_job()

    {

    try

    {

    SqlConnection jobConnection;

    SqlCommand jobCommand;

    SqlParameter jobReturnValue;

    SqlParameter jobParameter;

    int jobResult;

    jobConnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["SQL_Job"].ToString());

    jobCommand = new SqlCommand("sp_start_job", jobConnection);

    jobCommand.CommandType = CommandType.StoredProcedure;

    jobReturnValue = new SqlParameter("@RETURN_VALUE", SqlDbType.Int);

    jobReturnValue.Direction = ParameterDirection.ReturnValue;

    jobCommand.Parameters.Add(jobReturnValue);

    jobParameter = new SqlParameter("@job_name", SqlDbType.VarChar);

    jobParameter.Direction = ParameterDirection.Input;

    jobCommand.Parameters.Add(jobParameter);

    jobParameter.Value = "My Job";

    jobConnection.Open();

    jobCommand.ExecuteNonQuery();

    jobResult = (Int32)jobCommand.Parameters["@RETURN_VALUE"].Value;

    jobConnection.Close();

    switch (jobResult)

    {

    case 0:

    lblError.Text = "started successfully";

    break;

    default:

    lblError.Text = "failed to start";

    break;

    }

    }

    catch (SqlException ex)

    {

    ClientScript.RegisterClientScriptBlock(typeof(Page), "Error", "alert('" + ex.Message + "')", true);

    lblError.Text = ex.Message.ToString();

    return;

    }

    }

    If you need the result of the package try with sp_help_job @job_name = 'My Job'

  • #1CoolGuy (10/26/2007)


    Thanks Vidhya but, If I am starting the job with sp_start_job I don't see how he "on error" in the SSIS package will help me.

    I figure I can query the sysjobhistory table to determine the last execution status for that job, however, this seems like a problem that lots of you SQL Experts would have run into in the past and I was wondering if there was a cleaner/ better way to do this that I am not thinking of.

    Thanks

    The SSIS package is running as a step in a SQL Server Agent Job, correct? You set it up so that if the package fails, the job step fails. You then have the as part of the failed job step that it runs another job step that emails all the appropriate parties that the process failed.

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply