SSIS – Creating a process which will check a condition, if it is not ready wait and try again.
So we had a requirement where we wanted to create a waiting for loop, so that if it was not ready it would wait, try again, and if still not ready wait and try again until it got to the Max Number of tries. We also did not want it to error whilst waiting.
Example:
- Check to see if the database is online.
- Wait for 1 minute to retry.
- Try 10 times before failing
-
The first that we need to do, is to create some Variables which will help with the process.
-
From the above this is what they are for:
- Max_Count – This is how many times you want to retry
- State – This is our check to see if the databases are online or not.
- SuccessfulRun – This is used to see if it ran successfully
- Value_Counter – This is used as the counter.
-
Then below are the steps.
- The first thing is to assign a value to our Max_Count Variable.
-
This is done using an Execute SQL Task and assigned it with the following below:
- As you can see above in the SQLStatement we have set our Max_Count to 10
-
Then in the ResultSet we have mapped our results to our Variable
-
Next is where we configured our For Loop Container with the details shown below:
- As you can see above we have assigned it with our Value_Counter Variable.
-
You will note that in the EvalExpression we have got our Max_Count variable as well as the SuccessfulRun
not
equal to zero. This will be explained later how this value is derived.- NOTE: This is also required so that it will exit when it is actually
successful.
- NOTE: This is also required so that it will exit when it is actually
-
Next in our process within the For Loop is where we start with our Waiting Period as shown below using the Execute SQL Task
- All that we are doing here is using a TSQL
statement to wait for 1 minute. - NOTE: The reason we do this at the start, is so that if it has to loop through, it will wait on the second
loop before checking.
- All that we are doing here is using a TSQL
-
Next is where we have our condition (which based on our example is to check if the database is online) as shown below:
-
What our SQLStatement is doing above is checking to see the State of our database.
- And if it is online it will have a state of equal to zero
- And if it is online it will have a state of equal to zero
-
Then in the ResultSet we have mapped our results to our Variable
-
-
Next is where we have our Script Task which we use to populate the SuccessfulRun variable as shown below:
- We went into Edit the script with the following below:
- As you can see above we also assigned the same variable
SuccessfulRun to the ReadWriteVariables - NOTE: We did use the Microsoft Visual C# 2012 as the script
language. - From the above all that we added was to give the Variable SuccessfulRun = 1
-
And then we set our database
back into Multi User Mode after it was successfully back online as shown below: -
The final thing that we had to do, which actually ensure that it loops through without
erroring is to put in a precedence
constraint
between the Check if Database is Online and Check to see if Successful as shown below:-
We then configured it with the following for the precedence
constraint as shown below:
-
-
So once this is all done it looks like the following:
- You can now run this, potentially
changing your condition (Check if Database is Online) that you want to check for. As well as you can also change how long you want to wait for before
checking
again.