Insert resultset row in a forEach loop - sql

I was wondering if it is possible to insert the row being read by a for each loop in SSIS into another table. As the resultset is being processed by the foreach loop, i want to store the values in the table so they can be referenced again later in my project.
The result set is being stored in a global variable (object).
I'm not sure if this is clear. Please help me think through the problem. Thanks for your time!!!

You can add a DataFlow Task before the foreach Loop and Read the values from the Object variables as Source:
Using The SSIS Object Variable As A Data Flow Source
Creating a Source with the Script Component

Related

Variable values stored outside of SSIS

This is merely a SSIS question for advanced programmers. I have a sql table that holds clientid, clientname, Filename, Ftplocationfolderpath, filelocationfolderpath
This table holds a unique record for each of my clients. As my client list grows I add a new row in my sql table for that client.
My question is this: Can I use the values in my sql table and somehow reference each of them in my SSIS package variables based on client id?
The reason for the sql table is that sometimes we get request to change the delivery or file name of a file we send externally. We would like to be able to change those things dynamically on the fly within the sql table instead of having to export the package each time and manually change then re-import the package. Each client has it's own SSIS package
let me know if this is feasible..I'd appreciate any insight
Yes, it is possible. There are two ways to approach this and it depends on how the job runs. First is if you are running for a single client for a single job run or if you are running for multiple clients for a single job run.
Either way, you will use the Execute SQL Task to retrieve data from the database and assign it to your variables.
You are running for a single client. This is fairly straightforward. In the Result Set, select the option for Single Row and map the single row's result to the package variables and go about your processing.
You are running for multiple clients. In the Result Set, select Full Result Set and assign the result to a single package variable that is of type Object - give it a meaningful name like ObjectRs. You will then add a ForEachLoop Enumerator:
Type: Foreach ADO Enumerator
ADO object source variable: Select the ObjectRs.
Enumerator Mode: Rows in all the tables (ADO.NET dataset only)
In Variable mappings, map all of the columns in their sequential order to the package variables. This effectively transforms the package into a series of single transactions that are looped.
Yes.
I assume that you run your package once per client or use some loop.
At the beginning of the "per client" code read all required values from the database into SSIS varaibles and the use these variables to define what you need. You should not hardcode client specific information in the package.

dynamic ssis package

I need to create SSIS package for importing files from ftp server to table on Data Lake. The problem is that files can have different columns. For example File1 can have A,B,C,D,E columns, next file can have A,B,C just, next A,B,C,D,E,F and so on. What is the best way to approach this problem?
I m talking about different columns for source file and same destination table.
Thanks
Look into BiML, which dynamically creates packages based on meta data.
Add an Object Variable
Add a data flow:
Use this script component to get column names:
3.5 You might want to add a condition split or derived columns to monkey with the output.
Load the records into a recordset destination (use variable created in #1)
Add a ForEach and iterate through ADOObject
Add a variable to store each iteration
Create a variable to store SQL to pull your data set (Ex. "Select * from [" + variablecreatedInStep6 + "]"
Set your Source to use that variable
MAKE SURE EVERYTHING IS DELAYED VALIDATION AS THIS IS ALL DYNAMIC

loop row by row from an excel file map to variable

I have 4 columns in an excel file. I need to assign values from each row to the corresponding 4 variables so they can be replaced lated in a query i am doing on a server.
My question is: How to do that?
So far i tried doing an SQL task in which i create a table with 4 columns (having the same names as those in my excel file) and a task to transfer the content of the excel to a recordset destination which stores the results into a variable. I also created a foreach loop in which i am having my tasks. What am I missing, how can I do this?
Thanks
EDIT
please find below a screenshot from my project. This is the overview.
In "Execute SQL Task" there is a connection to excel and has the following statement
CREATE TABLE tempVariableMapping
(
AsofDate varchar(20),
Assump_Set varchar(20),
MarketName varchar(20),
Portname varchar(20)
);
Then in the transfer task (in the recordset destination), i'm assigning the variable name to User::RecordSetOutput which is a global variable of type object.
In foreach loop i'm using a foreach ado enumerator and pointing to that User::RecordSetOutput variable find below the variable mapping
Those 4 variables in variable mapping are those in which i want to pass the values from each row of the excel file.
The sequence container and create temp table are just dummy. Haven't figured out the correct way. Everything below that, works.
Sorry for the missunderstanding, hope this is enough to get the picture.
Thank you for your time and help

SSIS Script Component - only to change variables

I have a series of task that are very similar:
SELECT a,b FROM c
Lookup in another table and change value in column b.
Save new value back to c and if not match, send the result on to an error table.
That part is pretty straight forward and illustrated here:
Source ==> Lookup =match=> SQL Update command
=No match=> SQL Save Error command
(Hope you understand what I mean - but it works!)
I now have to repeat this a number of times, where my source-sql changes. So what I want to do is to insert a Script Component in front of the Source and set my User::Sql variable like:
Variables.Sql = "SELECT d, e FROM f"
All of the above is contained in a Data Flow. When I have created one I can then copy that one and only change the Sql variable in the script and then it should all work.
My problem is: When I insert the Script Command it asks me if it is a Source, Destination or Transscript script. And by only setting the variable it does not produce any rows for output and cannot connect to my Source.
Anyone know how to make that work?
(I have simplified the above. I actually want to update multiple variables and use those in my Source, Lookup and Error update as well - therefore it is not more simple just to change the SQL script in the initial Source! But being able to do the above, I will be able to achieve what I want :-))
You should set your variable containing the SQL query in the control flow, before you execute the dataflow.
Then you need to use that variable as an expression in your Dataflow. You can parametrize the query used in the lookup or any other parameters of your dataflow.
If your dataflows really have always the same structure, you could even generate a list of queries and call your dataflow task in a loop, preventing the duplication of the same tasks.

From an SSIS package how should I execute a sql script stored in a table as a varchar(max)?

I'm storing large (varchar(max)) SQL scripts in a table. I'd like to execute the scripts in an SSIS package.
Looking at other posts on this site it's easy enough to get the varchar(max) into an object variable. But then what to do? Is there a way for an Execute Sql Task (SQLSourceType of Variable) to specify an Object variable rather than a String variable?
Is there an approach that will work?
Here's how I might approach it:
Add a Data Flow task to your control flow
Add a Source (ADO.NET) that connects to your database
Create a Package level Object variable (for the next step)
Add a Recordset destination that populates your data into the Object variable created in the previous step
Back on the control flow:
Create a package level String variable for the "current" query (see next step)
Add a For Each ADO.NET enumerator
Connect the previous Data Flow task to the For Each task
Configure the For Each to use the Object variable as a source, and to store the column index with the SQL into the String variable
Add an Execute SQL task inside the For Each task
Configure it to execute a SQL Command from Variable, and pick the string variable containing the current query
Basically it will collect the queries from the table, then for each collected query, assign it to a variable, and then the Execute SQL command can pull the command text from that variable.