将包变量设置为数据库中的值 [英] set package variable to a value from database

查看:23
本文介绍了将包变量设置为数据库中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 SSIS 项目,我想在其中将我的包变量之一设置为我的数据库表中某个列的最大值.假设包变量名为 var.

I have an SSIS project in which I would like to set one of my package variables to the maximum value of some column in one of my database tables. Suppose the package variable is called var.

我的 SQL 看起来像:

my SQL would look like:

SET var = (SELECT max(some_column) from SomeTable)

这如何适应 SSIS 控制流/数据流?该 SQL 将执行什么类型的任务?我什至可以通过 SQL 访问包变量吗?

How does this fit into an SSIS Control Flow /Data Flow? What type of task would this SQL go into? Can I even access a package variable via SQL?

推荐答案

您可以使用执行 Sql 任务来完成此任务.语法会因您使用的数据库连接类型(OleDb 或 .Net)而异.

You can use an Execute Sql Task to accomplish this. The syntax will vary depending on the type of database connection you are using (OleDb or .Net).

如果您使用的是 OleDb 连接管理器,您的 sql 语句将如下所示:

If you are using an OleDb connection manager, your sql statement will look something like this:

SELECT ? = MAX(some_column) FROM some_table

然后选择参数映射"选项卡以映射您的参数/变量.从变量名称"字段的下拉列表中选择您的包变量.将 Direction 设置为 Output 并将 Parameter Name 设置为 0.(OleDb 连接上的变量从 0 开始按顺序映射)

Then select the Parameter Mapping tab to map your parameter/variable. Select your package variable from the dropdown for the "Variable Name" field. Set the Direction to Output and set the Parameter Name to 0. (variables on an OleDb connection are mapped in sequential order starting from 0)

如果您使用的是 .Net 连接管理器,您可以使用更熟悉的 @variableName 作为 sql 语句中的参数占位符,以及执行 SQL 任务中的参数名称.

If you are using a .Net connection manager, you can use the more familiar @variableName for the parameter place holder in your sql statement, as well as the Parameter Name in the Execute SQL task.

回应您的评论.查看直接从我的一个项目中提取的一些屏幕截图.请比较您正在做什么以确保我们在同一页面上.

In response to your comment. See a few screenshots pulled directly from one of my projects. Please compare what you are doing to make sure we are on the same page.

在这个镜头中,请注意执行 Sql 任务的连接类型,并注意 Sql 语句的最后一条语句,其中我选择了一个等于 sql 函数的参数.

In this shot, note the connection type of the Execute Sql Task and note the last statement of the Sql Statement where I am selecting a parameter to be equal to a sql function.

在下一个镜头中,看看我如何配置第二个参数,确保参数方向设置为输出.该参数命名为 1,因为它是 sql 语句中的第二个参数/参数持有者.

In this next shot, see how I am configuring the second parameter, making sure that the parameter direction is set to output. The parameter is named 1 as it is the second parameter/parameter holder in the sql statement.

这篇关于将包变量设置为数据库中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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