我有一個調用子包的父包。如果子包失敗,則爲子包顯示完整的錯誤詳細信息,但父包僅顯示「任務執行包失敗」。當孩子包失敗時SSIS通過FULL錯誤到父包
如何讓父包從子包中獲取完整的錯誤消息,以便我可以正確地獲取父包中的完整錯誤詳細信息?
我有一個調用子包的父包。如果子包失敗,則爲子包顯示完整的錯誤詳細信息,但父包僅顯示「任務執行包失敗」。當孩子包失敗時SSIS通過FULL錯誤到父包
如何讓父包從子包中獲取完整的錯誤消息,以便我可以正確地獲取父包中的完整錯誤詳細信息?
一種方法是讓子包填充帶有錯誤消息的變量,然後在父級的OnError(或OnPostExecute)處理程序中讀取該變量。
SOLUTION:
如何從子包獲取錯誤詳細信息。
此解決方案將會收到子包所包含的任何錯誤,並將錯誤消息傳遞給父包。然後,父包會收到它收到的錯誤,並在父包執行結果中發佈完整的詳細信息。
注意:編寫邏輯時,子包仍然可以自行運行(而不是作爲子包),並且沒有父包中缺少變量名的錯誤。此外,如果您從父包運行子包但不想從子包捕獲錯誤,則不要包含變量名或OnError事件處理程序,並且它不會以這種方式爲缺少的變量導致任何錯誤。
爲整個子包創建一個OnError事件處理程序。
將腳本任務添加到事件處理程序。
a。將它傳遞給只讀變量: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();
}
父包中創建一個字符串變量:OnError_ErrorDescription_FromChild
在父包創建的OnError事件處理程序整個軟件包並添加一個腳本任務。
在腳本任務(如你與上面的子包一樣)傳遞變量爲只讀:用戶:: OnError_ErrorDescription_FromChild
在腳本任務執行以下操作:
// 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
我知道如何從孩子分享一個變量給父母(我正在使用腳本任務)。 您是否有更多詳細信息和/或如何使用錯誤詳細信息執行此操作的示例? – Brad
請參閱此問題的答案以獲得一些示例鏈接:http://stackoverflow.com/questions/9154672/which-ssis-system-variable-holds-error-text –
另請參見:http://www.sqlservercentral .com /論壇/ Topic451275-148-1.aspx –