Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional SSIS - execute one task or another based on the result

I have the following tasks in SSIS:

enter image description here

In Check Stock task, I execute a stored procedure which returns 0 or 1:

    CREATE PROCEDURE [dbo].[SP_CheckStockAvailability]
AS

BEGIN

DECLARE @ItemGID nvarchar(250)=(SELECT TOP (1) ItemGID FROM XMLOrderlines WHERE Comparison='0')

SELECT CASE 
WHEN @ItemGID IS NOT NULL
THEN CAST (0 AS bit)
ELSE CAST (1 AS bit)
END AS Comparison
FROM XMLOrderlines 

END
GO

I would like to execute Reject Order (on the right) task if the result is 1 and, if not, to execute the one from the left. I set to export the result of the procedure in a variable, Boolean data type with a default value of "False".

If I edit the precedence constraint and I set Expression as evaluation operation and then choose the variable from the previous task, either way it does not go to the next task that is supposed to. What am I missing? I tried what I found on the web but nothing helped me. Thanks!

enter image description here

like image 999
cdrrr Avatar asked Jan 15 '18 14:01

cdrrr


People also ask

What conditions can be given to precedence constraints in SSIS?

The precedence constraint uses a constraint value, an expression, both, or either to determine whether the constrained executable runs. If the precedence constraint uses an execution result, you can specify the execution result to be success, failure, or completion.

How do you write conditional condition split in SSIS?

Add a SSIS Conditional Split Transformation to the Data Flow The Conditional Split Transformation task checks for the specified condition. It moves the data to an appropriate destination depending upon the condition. Drag the SSIS Conditional Split task from the SSIS Toolbox.

What is parallel execution in SSIS?

It defines how many tasks (executables) can run simultaneously. It defaults to -1 which is translated to the number of processors plus 2.

Which SSIS task is used to execute another package from with the same project?

The Execute Package task in SSIS allows us to call packages present in the Same Project, File system, and SQL Server.


1 Answers

Solution

You have to set the following values:

Reject Order

Evaluation operation:   Expression and Constraint
Value:                  Success
Expression:             @[User::Result] = 1
                     OR 
                        @[User::Result]

Accept Order

Evaluation operation:   Expression and Constraint
Value:                  Success
Expression:             @[User::Result] = 0 
                     OR 
                        !@[User::Result]

Screenshot

enter image description here

Suggestions

I think it is better to add a TRY...CATCH block to your procedure, so if it encounters an error the result will be 1 and the Order is rejected:

CREATE PROCEDURE [dbo].[SP_CheckStockAvailability]
AS

    BEGIN

    BEGIN TRY  

        DECLARE @ItemGID nvarchar(250)=(SELECT TOP (1) ItemGID FROM XMLOrderlines WHERE Comparison='0')

        SELECT CASE 
        WHEN @ItemGID IS NOT NULL
        THEN CAST (0 AS bit)
        ELSE CAST (1 AS bit)
        END AS Comparison
        FROM XMLOrderlines 

    END TRY 
    BEGIN CATCH

        SELECT 1 AS Comparison

    END CATCH

    END
GO
like image 80
Hadi Avatar answered Oct 20 '22 18:10

Hadi