SQL SSIS 结果集、Foreachloop 和变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20643081/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SSIS Result set, Foreachloop and Variable
提问by user3112621
I'm having an issue and I don't know if this is possible to do.
我遇到了问题,我不知道这是否可行。
I have a simple SQL task.
我有一个简单的 SQL 任务。
SELECT Name, email FROM table_name
SELECT Name, email FROM table_name
Using a result set, I'm trying to pass it to a variable and use it on a email task with a foreach loop but i having no luck getting this to work. If I do just one column, it works fine. My issue is multiple columns.
使用结果集,我试图将它传递给一个变量,并在带有 foreach 循环的电子邮件任务中使用它,但我没有运气让它工作。如果我只做一列,它工作正常。我的问题是多列。
Thanks for the help in advance.
我在这里先向您的帮助表示感谢。
回答by user3112621
I figured out my own question. I will add it here in case someone else have the same issue.
我想出了我自己的问题。我会在这里添加它以防其他人有同样的问题。
On the SQL query the first column should be your key result. for example mine was the email.
In the SQL Taskunder General, set Resultsetto "Full Result rest"
In the SQL Taskunder Result Set, set variable with "0" as result name, create a variable as "Object" data type for the email column and click ok to save and exit. Example: email_Ob
By going to the variablewindow, create more variable for the other columns as Object and since I'm using this for email task I need to convert from Object to String, so I need to create another variable as string for each one
Example:
**Variable Name** **Datatype** email_Ob Object Name_Ob Object email_St String Name_St String
Foreach Loop taskUnder Collectionchange the following:
Enumerator: Foreach ADO Enomator
ADO object Source variable: Select the key variable (email_Ob)
Select Rows in the first table
Foreach Loop taskunder Variable Mappingsadd the variable with Stringdata type the same order as on your SQL query. Click OK to save and exit.
Now you can add the Email Taskinside Foreach Loopand use those String variable as part of the email or you can use it for any other task.
在 SQL 查询中,第一列应该是您的关键结果。例如我的是电子邮件。
在General下的SQL Task 中,将Resultset设置为“Full Result rest”
在“结果集”下的“ SQL任务”中,将变量设置为“0”作为结果名称,为电子邮件列创建一个“对象”数据类型的变量,然后单击“确定”保存并退出。示例:email_Ob
通过转到变量窗口,为其他列创建更多变量作为对象,并且由于我将其用于电子邮件任务,因此我需要从对象转换为字符串,因此我需要为每个变量创建另一个变量作为字符串
例子:
**Variable Name** **Datatype** email_Ob Object Name_Ob Object email_St String Name_St String
集合下的Foreach 循环任务更改以下内容:
枚举器:Foreach ADO枚举器
ADO 对象源变量:选择关键变量 (email_Ob)
选择第一个表中的行
变量映射下的Foreach 循环任务添加字符串数据类型的变量,其顺序与 SQL 查询中的顺序相同。单击“确定”保存并退出。
现在您可以在Foreach 循环中添加电子邮件任务并将这些字符串变量用作电子邮件的一部分,或者您可以将其用于任何其他任务。
Hope this help and if you have any questions feel free to ask. Doing this way, I was able to add more columns as need it.
希望这会有所帮助,如果您有任何问题,请随时提出。这样做,我可以根据需要添加更多列。