在 Foreach 循环中增加一个变量并使用它-SSIS [英] Increment a variable within a Foreach Loop and use it-SSIS

查看:33
本文介绍了在 Foreach 循环中增加一个变量并使用它-SSIS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包,我正在使用 foreach 循环遍历数据库.我正在传递一个字符串,它在所有数据库中循环.一切都很完美,直到这里.

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.

我想要实现的是,对于它循环的每个数据库,它应该将变量增加 1.假设我必须循环遍历 5 个数据库.并且包级变量(myvariable = 24)被声明为 24.对于它循环的每个数据库,它应该将变量增加 1.

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.

为此,我在 foreachloop 容器中创建了一个脚本任务.ReadWriteVariables 作为 myvariable.在脚本编辑器中.我有以下代码

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++
       }

然后我将这个递增的值传递给一个存储过程.但它没有增加.它仍然采用默认值 24.任何想法如何在 foreachloop 容器中增加变量?

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?

推荐答案

您创建的变量 varbl 仅存在于脚本上下文中.

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

要增加 SSIS 变量,您需要执行类似操作

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;
}

或者变量User::myvariable不会被改变

这篇关于在 Foreach 循环中增加一个变量并使用它-SSIS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆