Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I would like to add a condition to a sequence in my SQL Server 2008 Maintenance Plan. This condition is based on the variable set by a T-SQL Statement Task:

declare @primary bit = 0
select @primary=1
from sys.database_mirroring
where mirroring_role = 1

How can I perform this task through the precedence constraint editor?

share|improve this question

3 Answers 3

up vote 2 down vote accepted

I agree with SQLChicken. You won't be able to check the variables via a simple maintenance plan you create in SQL Management Studio. You will need to either script it out and put it in a SQL job or create a full-on SSIS package. SSIS will provide you with all of the maintenance plan tasks that are available via SSMS as well as all of the other benefits of SSIS including package variables that you can use in your precedence constraints.

share|improve this answer
    
Thanks, indeed, I had to go through SSIS to add variables. I did manage to edit the maintenance plan on SSIS and upload it back to SQL Server, but there is no real added value to use maintenance plans now... –  Idriss Aug 13 '09 at 10:10

Honestly if you're going to get into advanced tweaking like that you're better off scripting a solution and then scheduling it via Agent. Maint Plans, while nice for really simple tasks, really has its limitations.

share|improve this answer

I have a solution to this however it is not elegant. This works, but it is a cludge and it would never be considered a "best practice".

The solution involves using artificially generated errors to control flow, and tweaking package properties so that the overall error condition of the calling job reports success or failure the way we would want.

First, create an Execute T-SQL Statement task with your conditional check. From the example in the original question, this would look like this:

if not exists (select * from sys.database_mirroring where mirroring_role = 1)
   begin
   raiserror('not primary', 16, 1)
   end

This code generates an error if this is not the mirroring primary, and no error if it is the primary. Next, create second Execute T-SQL Statement task and link it to the first one with a Success condition. If this is not the mirror primary we will not go to this second task, and the sequence will end. If this is the primary we will proceed to this second task. The second task generates a dummy error using code like this:

raiserror('dummy error', 16, 1)

Now create your third task and link it to the second with a Failure condition. In this third task, do whatever it is you wanted to do if the condition in the first task was true. This can be another Execute T-SQL Statement, a Backup task, Update Statistics task, or whatever. If the condition was false, we will leave the sequence after the first task. If the condition was true we will progress through the dummy error and on to this third task which actually does the work we want done.

The reason for the dummy error in task 2 is to make the final error state of the calling job report what we want it to, and there are some other package properties we also need to tweak to make this work. Go to the properties window (if it isn't open, open it from the right click menu on any one of the tasks) and click on the dropdown at the top. This lists all the maintenance plan elements that you can change properties for. Click on Subplan_1 Sequence where Subplan_1 is the name of the subplan you are working in. Change FailParentOnFailure to False. This will keep the calling job from reporting an error when we generate the conditional error in task 1 or the dummy error in task 2. Next, go to properties for MyPackage Package where MyPackage is the name of the package you are working in. Change the MaximumErrorCount to 2. This will cause the calling job to report success when there is only one error generated by the dummy error in task 2, but report failure if the third task also generates an error. It will also report success if the only error generated is by the conditional check in task 1.

That's it, I hope someone finds this useful.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.