Showing posts with label databases. Show all posts
Showing posts with label databases. Show all posts

Thursday, March 29, 2012

ForEach Trapping an Error and Continuing

I have a ForEach loop that processes a list of databases. Inside the loop I many steps, one of which is a sequence that contains two steps. Either of these steps may fail (they are attempting to start mirroring and could fail for any number of reasons). I would like to trap this error and ignore it so the For loop will continue, but still fail if other steps than this one fail. The only thing I've been able to do so far is to tell the whole loop to continue through some insane number of errors. Is there a way to identify or actually ignore the error? In the sequence I have have on completion and from the sequence to the next step (which checks if mirroring actually started) is running on completion.

Thanks.

I found a solution using SQL Server.

BEGIN TRY
ALTER DATABASE AdventureWorks SET Partner='http://TEST'
END TRY
BEGIN CATCH
END CATCH

This will prevent the error from being seen by SSIS. But for other errors this will not work (such as SELECT * FROM person.contacts) where contacts does not exist in the adventureworks database (person.contact does).

I'd still be interested in any feedback on ways to selectively trap and ignore errors and get the for loop to continue.

Larry

|||

For selectively ignoring an error, one approach I've used it to modify the MaximumErrorCount to greater than 1 (say 1 billion) on the task (or container) I want to selectively ignore errors on. Then, put an error handler on the task or container which basically sets a variable for fatal errors and use expression based precedence constraints rather than success/failure/completion precedence constraints.

Public Sub Main()

' Don't propagate error message up the chain, is this propagated

Dim errorMessage As String

Dts.Variables("Propagate").Value = False

'inspect error message

errorMessage = CType(Dts.Variables("ErrorDescription").Value, String)

If errorMessage.Contains("really bad error here") Then

Dts.Variables("FatalError").Value = True

End If

Dts.TaskResult = Dts.Results.Success

End Sub

Sunday, February 19, 2012

For Each across list of databases

I need to get records from multiple databases.

In my main database, I have a list of databases related to seperate business units.

For each of those databases, I need to get a list of values from a table (the table exists in each database).

Basically

foreach database in a list
Do a Lookup
end

Possible?

The option I would consider would be to put a Data Flow Task in the PreExecute Event Handler of the For Each Loop Task to loop through the databases you need to access.

First, parameterize the Connection String of your source main Data Flow DB source (not the Event Handler DB source) with a variable. Here's an example of what I'm doing within an expression for the ConnectionString:

"Data Source=" + @.[User::masterServer] + ";Initial Catalog=" + @.[User::masterDatabase] + ";Provider=SQLNCLI.1;Integrated Security=SSPI;Auto Translate=False;"

Create a PreExecute Event Handler Data Flow Task in which you select the database names from sysdatabases with a OLE DB source (not the same source you're using in the main Handler Data Flow) connected to the server and write it to a RecordSet Destination. You'll have to define a new variable of type Object as the RS destination.

Then, specify an ADO Enumerator as the Enumerator of your For Each Loop and map the database name to the variable that you're using to parameterize the Connection String of your non-Event Handler from the RecordSet variable.

|||Thanks!

That looks like exactly what I'd like to do.

There are several details in your response that I'm not sure about. But I'm going to try to work them out on my own before asking for more help.|||

Dear mr_superlove,

I have been looking to use For Each Across a list of database. Your reply seems very helpful..

I am new to SSIS. I tried to do what you said. I am missing something to get it working. Can you elaborate the procedure you wrote in step by step way.

I would really appreciate you effort.

Thank you,

Sanjeev