user1162670
user1162670

Reputation: 73

Increment a variable within a Foreach Loop and use it-SSIS

I have a package i am using the foreach loop to loop through the databases. I am passing a string and it loops though all the databases. Everything is perfect till here.

What i want to achieve is that for each databases it loops, it should increment a variable by 1. suppose if i have to loop through a total of 5 databases. And a package level variable(myvariable =24) is declared as 24. for each databases it loops it should increment the variable by 1.

For that i have created a script task inside the foreachloop container. ReadWriteVariables as myvariable. In the script editor. I have the following code

public void Main()
        {
     int varbl = Convert.ToInt32(Dts.Variables["User::myvariable"].Value.ToString());
     varbl++
       }

and then i am passing that incremented value to a storedprocedure. But its not incrementing. It is still taking the default value of 24. Any ideas how can i increment a variable in foreachloop container?

Upvotes: 3

Views: 14011

Answers (1)

cfrag
cfrag

Reputation: 746

The variable varblthat you created only exists in the script context.

To increment the SSIS variable you need to do something like

public void Main()
{
    int varbl = Convert.ToInt32(Dts.Variables["User::myvariable"].Value.ToString());
    varbl++;
    Dts.Variables["User::myvariable"].Value = varbl;
}

or the variable User::myvariable will not be changed

Upvotes: 11

Related Questions