简体   繁体   中英

SSIS How to pass variable/row count from one data flow process to another in the same control flow

Sorry I am new to SSIS. Using Visual Studio 2017. In my SSIS package, in my Control Flow screen, I have just one Data Flow Task rectangle, that I labeled Appointments. When you click on that, you are taken to the Data Flow tab, where really everything happens here. I have two different processes or paths here. So for Path 1, I have a OLE DB Source. This runs a SQLSelect statement that pulls data from some tables off a SQL Server. Then there is a green connector from this to a Row Count transformation rectangle, and then there is a green connector from that to a Flat File Destination rectangle. The Flat File destination is set up to get the output from the SQL statement, create a file on my hard drive, and put the results of the Select statement into that file. I call this file a “data” file. That is it for this process. My hope is to somehow save the record count of the Select statement. Now, on the same tab, I have another process we will call Path 2. I have an OLE DB Source. In this source, I have a SQL Select statement that just creates one record with 6 fields. All the fields are hard coded except one. This field is called RecordCount. So what needs to dynamically go into this field, is the row count from Path 1 (for now, I just have it hard coded to zero). I have a green connector from this OLE DB Source that goes to a Flat File Destination rectangle. This destination is set up to get the output from the (mostly) hard coded SQL statement, create a file on my hard drive, and put that one record into it. I call this file a “control” file. I have 3 connection managers created, one that connects to the SQL Server, and one for each Path on this tab. How can I get the row count from the first Path into the field in the file created by the second Path? I have tried several possibilities and have googled until I am blue in the face, but I cannot get anything to work. Being a newbie, I hesitate to tell you what all I have tried – so feel free to suggest anything, assume I know nothing. Keep in mind that I may in the future create a Path 3 and a Path 4 on the same tab, where the row count from Path 3 goes to Path 4 (don't want to be using here the rowcount value from Path 1) – how will this affect things? Or would I need Path 3 and Path 4 to be related to a new Data Flow Task on the Control Flow screen?

错误

The Row Count transformation requires an SSIS Variable. Double click it and see what value you put in there, something like @User::Variable

When the data flow finishes, the value of that variable will contain the number of rows that passed through the component.

That is all that you want in your data flow. The secondary path you describe, you will want to add that to a new data flow. Draw an On Completion arrow from Data Flow 1 to Data Flow 2. And the same for your future steps 3 and 4. Make the Dataflow focus on solving a single business problem (Export Sales to Text file).

Now that the first data flow has completed, the value of the SSIS variable will contain a value so you're going to use that in Data Flow 2 to write your one row of data to a text file.

In your OLE DB Source, you'll use a parameter like

DECLARE @RowCount int = ?;
SELECT GETDATE() AS CurrentDate, 'SalesExport' AS ExportType, @RowCount AS ExportCount;

That ? is the placeholder for an OLE DB Parameter and if you click the Parameters button, you'll be able to specify that parameter 0 maps to @User::Variable

在此处输入图像描述

When that runs, you'll get a flat file with your one row of data and the actual value computed in data flow 1.

Lather, rinse, repeat the pattern for your exports. Do create an SSIS variable for each row count you need to keep track of so @User::CountSales , @User::CountConsumers , @User::CountProviders so that way you don't run into trouble later by re-using a variable before you were ready.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM