子包失败时的 SSIS 将 FULL 错误传递给父包

SSIS when child package fails pass FULL error to parent package

我有一个调用子包的父包。如果子包失败,则会显示子包的完整错误详细信息,但父包只会显示 "Task Execute Package Failed".

如何让父包从子包中获取完整的错误信息,以便正确抓取父包中的完整错误详细信息?

一种方法是让子包使用错误消息填充变量,然后在父包的 OnError(或 OnPostExecute)处理程序中读取该变量。

解决方案:

如何从子包中获取错误详细信息。

此解决方案将处理子包出现的任何错误并将错误消息向上传递给父包。然后父包将接受它收到的错误和 post 父包执行结果中的完整详细信息。

注意:编写逻辑是为了使子包本身仍然可以 运行(而不是作为子包),而不会因父包中缺少变量名而出现任何错误。此外,如果您 运行 来自父包的子包但不想从子包中捕获错误,则不包括变量名或 OnError 事件处理程序,它不会因缺少变量而导致任何错误这样。

  1. 为整个子包创建了一个 OnError 事件处理程序。

  2. 将脚本任务添加到事件处理程序。

    一个。以只读方式将变量传递给它:System::ErrorDescription,System::SourceName,System::PackageName

    b。在脚本任务中执行此操作(注释应详细说明它在做什么):

        // build our the error message
        string ErrorMessageToPassToParent = "Package Name:  " + Dts.Variables["System::PackageName"].Value.ToString() + Environment.NewLine + Environment.NewLine +
            "Step Failed On:  " + Dts.Variables["System::SourceName"].Value.ToString() + Environment.NewLine + Environment.NewLine +
            "Error Description:  " + Dts.Variables["System::ErrorDescription"].Value.ToString();
    
    
    
        // have to do this FIRST so you can access variable without passing it into the script task from SSIS tool box
        // populate collection of variables. This will include parent package variables.
        Variables vars = null;
        Dts.VariableDispenser.GetVariables(ref vars);
    
    
        // checks if this variable exists in parent first, and if so then will set it to the value of the child variable 
        //  (do this so if parent package does not have the variable it will not error out when trying to set a non-existent variable)
        if (Dts.VariableDispenser.Contains("OnError_ErrorDescription_FromChild") == true)
        {
    
            // Lock the to and from variables. 
            // parent variable
            Dts.VariableDispenser.LockForWrite("User::OnError_ErrorDescription_FromChild");
    
            // Need to call GetVariables again after locking them. Not sure why - perhaps to get a clean post-lock set of values.
            Dts.VariableDispenser.GetVariables(ref vars);
    
            // Set parentvar = childvar
            vars["User::OnError_ErrorDescription_FromChild"].Value = ErrorMessageToPassToParent;
    
            vars.Unlock();
        }
    
  3. 在父包中创建一个字符串变量:OnError_ErrorDescription_FromChild

  4. 在父包中为整个包创建一个 OnError 事件处理程序并向其添加一个脚本任务。 (就像你对上面的子包所做的那样)

  5. 在脚本任务中将变量作为只读传递:User::OnError_ErrorDescription_FromChild

  6. 在脚本任务中执行以下操作:

        // get the variable from the parent package for the error
        string ErrorFromChildPackage = Dts.Variables["User::OnError_ErrorDescription_FromChild"].Value.ToString();
    
        // do a check if the value is empty or not (so we know if the error came from the child package or occurred in the parent package itself
        if (ErrorFromChildPackage.Length > 0)
        {
            // Then raise the error that was created in the child package
            Dts.Events.FireError(0, "Capture Error From Child Package Failure",
                    ErrorFromChildPackage   
                    , String.Empty, 0);
    
        } // end if the error length of variable is > 0