The Row Count Transformation in SSIS used to counts the rows as they pass through a data flow and store the final count in a variable. For example, If we want to inform the Manager to notify the number of rows inserted, then we can use SSIS Row Count Transformation for counting the rows. Then use the Script Task to send an e-mail message to the Manager.
In SQL Server Integration Services, Row Count Transformation will not update the variable value until the last row of the source data has passed through this SSIS Transformation. So, we can’t use the updated values in the middle. Let us design one package so that we can understand this SSIS Row Count transformation better.
Row Count Transformation in SSIS Example
STEP 1: Drag and drop the data flow task from the toolbox to control flow and rename it as Row Count Transformation in SSIS
STEP 2: This transformation stores counted rows information in a variable only, so before using this transformation, we need to create a variable on the Variables tab and add an integer variable to the package.
Right-click on the SSIS Control Flow region will open the context menu with multiple options. Select the Variables option from the context menu to create a variable.
In this case, we’ve chosen to add a variable named NumberOfRows of integer type and assigned value to 0.
Double click on the data flow task will open the data flow tab.
STEP 3: Drag and drop OLE DB Source, Row Count Transformation from the SSIS toolbox to the data flow region
STEP 4: Double click on OLE DB source in the data flow region will open the connection manager settings and provides space to write our SQL statement.
SQL Command we used in the above figure
USE AdventureWorksDW2014 GO SELECT [EnglishCountryRegionName] ,[StateProvinceName] ,[City] ,[PostalCode] ,RESELLER.[UnitPrice] ,RESELLER.[ProductStandardCost] ,RESELLER.[TotalProductCost] ,RESELLER.[SalesAmount] ,RESELLER.[TaxAmt] FROM [DimGeography] INNER JOIN [FactResellerSales] AS RESELLER ON [DimGeography].[SalesTerritoryKey] = RESELLER.[SalesTerritoryKey] ORDER BY [EnglishCountryRegionName],[StateProvinceName]
STEP 5: Click on columns tab to verify the columns. In this tab we can uncheck the unwanted columns also.
STEP 6: The final step is to configure the Row Count transformation. Double click on the SSIS Row Count Transformation will open small Row Count Window to select the User-defined Variable from the drop-down. Here we are choosing the already created variable (NumberOfRows).
Or you can configure the SSIS Row Count Transformation with a more traditional approach. Right-click on the Row Count Transformation will open the context menu with multiple options. Here select the Show Advanced Editor option from it.
It opens the Advanced Editor of the SSIS Row Count Transformation. Within the Component Properties tab choose the VariableName property of the Row Count Editor, shown in Figure below and associate the variable we created before (NumberOfRows)
STEP 7: Go to Control flow region and Drag and drop the Script Task from the toolbox to the Control Flow Region. Next, double-click on it to configure
STEP 8: Select the user-defined variable as a read-write variable
STEP 9: Click ok and then click on Edit Script button to open class file
Type the following lines of code in the Main method
String Message = Dts.Variables["User::NumberOFRows"].Value.ToString(); MessageBox.Show(Message);
NOTE: Here, I used the C# programming language code, and I am sorry for VB.Net people. I hope you can understand this code.
Close the class file, and Let’s run the SSIS Row Count Transformation package. From the below screenshot, you can observe that it is displaying the Message Box with the Number of Rows information.
Let us see the data Flow region whether the SSIS Row Count transformation is giving the correct Result or not
Remember, Until you click the ok button, Script task will be in running mode. Once you close the Message Box, then the Green Tick Mark will be shown
You can observe the message box displaying the number of rows passed through that transformation.
NOTE: This SSIS Row Count transformation has one input and one output. It does not support an error output.