简体   繁体   English

在SSIS中的查询中使用变量

[英]Using a variable in a lookup in SSIS

I am trying to do a loop through a few data flow tasks. 我正在尝试通过一些数据流任务进行循环。 With each loop it increases the date variable by 1 (which is stored in a variable). 对于每个循环,它将日期变量增加1(存储在变量中)。 Inside the data flow tasks I have a lookup task that is supposed to use the variable which is being incremented as part of the sql statement, something like this: 在数据流任务内部,我有一个lookup任务,该任务应该使用作为sql语句一部分递增的变量,如下所示:

Select
    *
From
    Table
Where
    Date = @[User::Date]

But this doesn't work, I get this error: 但这不起作用,我得到这个错误:

Must declare the scalar variable "@".

Any help in trying to get this variable into the sql of the lookup would be greatly appreciated. 试图将此变量放入lookup的sql中的任何帮助将不胜感激。 I've also tried using parameters within the lookup but also get an error saying there isn't enough parameters 我也尝试在lookup使用参数,但也收到错误消息,指出参数不足

Use a derived column before the lookup and the set the value of the new column to the variable. 在查找之前使用派生列,并将新列的值设置为变量。 Then in the lookup transform you can just do a straight mapping with the new derived column and the date column in your table and lookup intended columns. 然后,在查找转换中,您可以使用表中的新派生列和日期列以及要查找的列进行直接映射。

In your case it looks like you would need to use SQL due to the nature of your requirement. 在您的情况下,由于需求的性质,您似乎需要使用SQL。 Hence after changing your Cache Mode to Partial Cache, go to advanced tab and change your SQL to - 因此,在将缓存模式更改为部分缓存后,请转到高级标签,然后将SQL更改为-

Select * 
From Table 
Where Date <> ?

Click on parameters button and map your derived column to Parameter0. 单击参数按钮,然后将派生的列映射到Parameter0。 Hence the Lookup Match output would now essentially give you all the dates that does not match your derived column that is set by the variable @[User::Date]. 因此, 查找匹配输出现在将为您提供与变量@ [User :: Date]设置的派生列不匹配的所有日期。

You could use a logical expression in a derived column component to check the dates and assign a bool value to a new column then use a conditional split to parse records and modify the data flow. 您可以在派生的列组件中使用逻辑表达式来检查日期,并为新列分配布尔值,然后使用条件拆分来解析记录并修改数据流。

Image: Derived column expression example 图像:派生列表达式示例

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM