Tuesday, July 11, 2017

How to control SSIS package flow based on record count returned by a query?

One option would be to make use of precedence constraint in conjunction with Execute SQL task to achieve this functionality. Here is an example of how to achieve this in SSIS 2008 R2.
I created a simple table based on the information provided in the question.
Create table script:
CREATE TABLE dbo.AR_Sale(
    Id int NOT NULL IDENTITY PRIMARY KEY,
    Item varchar(30) NOT NULL,
    Price numeric(10, 2) NOT NULL,
    Processed bit NOT NULL
) 
GO
Then populated the new table with some sample data. You can see that one of the row has Processed flag set to zero.
Populate table script:
INSERT INTO dbo.AR_Sale (Item, Price, Processed) VALUES
    ('Item 1', 23.84, 1),
    ('Item 2', 72.19, 0),
    ('Item 3', 45.73, 1);
On the SSIS package, create the following two variables.
  • Processed of data type Int32
  • SQLFetchCount of data type String with value set to SELECT COUNT(Id) ProcessedCount FROM dbo.AR_Sale WHERE Processed = 0
Variables
On the SSIS project, create a OLE DB data source that points to the database of your choice. Add the data source to the package's connection manager. In this example, I have used named the data source as Practice.
On the package's Control Flow tab, drag and drop Execute SQL Task from the toolbox.
Configure the General page of the Execute SQL Task as shown below:
  • Give a proper Name, say Check pre-execution
  • Change ResultSet to Single row because the query returns a scalar value
  • Set the Connection to the OLE DB datasource, in this example Practice
  • Set the SQLSourceType to Variable because we will use the query stored in the variable
  • Set the SourceVariable to User::SQLFetchCount
  • Click Result Set page on the left section
Execute SQL Task - General
Configure the Result Set page of the Execute SQL Task as shown below:
  • Click Add button to add a new variable which will store the count value returned by the query
  • Change the Result Name to 0 to indicate the first column value returned by query
  • Set the Variable Name to User::Processed
  • Click OK
Execute SQL Task - Result Set
On the package's Control Flow tab, drag and drop Send Mail Task and Data Flow Task from the toolbox. The Control Flow tab should look something like this:
Control Flow tab
Right-click on the green arrow that joins the Execute SQL task and Send Mail Task. Click Edit... the Green Arrow is called as Precedence Constraint.
Right-click constraint 1
On the Precedence Constraint Editor, perform the following steps:
  • Set Evaluation operation to Expression
  • Set the Expression to @[User::Processed] == 0. It means that take this path only when the variable Processed is set to zero.
  • Click OK
Equal to zero
Right-click on the green arrow that joins the Execute SQL task and Data Flow Task. Click Edit... On the Precedence Constraint Editor, perform the following steps:
  • Set Evaluation operation to Expression
  • Set the Expression to @[User::Processed] != 0. It means that take this path only when the variable Processed is not set to zero.
  • Click OK
Not equal to zero
Control flow tab would look like this. You can configure the Send Mail Task to send email and the Data Flow Task to update the data according to your requirements.
Control Flow final
When I execute the package with the data set to based on the populate table script, the package will execute the Data Flow Task because there is one row that is not processed.
Data Flow Task logic
When I execute the package after setting Processed flag to 1 on all the rows in the table using the script UPDATE dbo.AR_Sale SET Processed = 1, the package will execute the Send Mail Task.
Send Mail Task logic
Hope that helps.

No comments:

Post a Comment

  How to Change Instance Type & Security Group of EC2 in AWS By David Taylor Updated April 29, 2023 EC2 stands for Elastic Compute Cloud...