SSIS Foreach Loop failure

时间:2015-05-04 19:52:42

标签: sql-server foreach ssis

I have created a lookup for a list of IDs and a subsequent Foreach loop to run an sql stmt for each ID.

enter image description here

My variable for catching the list of IDs is called MissingRecordIDs and is of type Object. In the Foreach container I map each value to a variable called RecordID of type Int32. No fancy scripts - I followed these instructions: https://www.simple-talk.com/sql/ssis/implementing-foreach-looping-logic-in-ssis-/ (without the file loading part - I am just running an SQL stmt).

It runs fine from within SSIS, but when I deploy it my Integration Services Catalogue in MSSQL it fails.

This is the error I get when running from SQL Mgt Studio:
enter image description here

I thought I could just put a Precendence Constraint after MissingRecordsIDs get filled to check for NULL and skip the Foreach loop if necessary - but I can't figure out how to check for NULL in an Object variable?

Here is the Variable declaration and Object getting enumerated: enter image description here

And here is the Variable mapping: enter image description here

The SQL stmt that is in 'Lookup missing Orders':

 select  distinct cast(od.order_id as int) as order_id
 from invman_staging.staging.invman_OrderDetails_cdc od
 LEFT OUTER JOIN invman_staging.staging.invman_Orders_cdc o
 on o.order_id = od.order_id and o.BatchID = ?
 where od.BatchID =  ?
 and o.order_id is null
 and od.order_id is not null

In the current environment this query returns nothing - there are no missing Orders, so I don't want to go into the 'Foreach Order Loop' at all.

2 个答案:

答案 0 :(得分:0)

这是微软知道的一个已知问题:https://connect.microsoft.com/SQLServer/feedback/details/742282/ssis-2012-wont-assign-null-values-from-sql-queries-to-variables-of-type-string-inside-a-foreach-loop

我建议将 ISNULL(RecordID,0)添加到查询中,并将表达式设置为组件“Load missing Orders”,以便仅在RecordID!= 0时启用它

答案 1 :(得分:0)

在我的情况下,它不是NULL导致问题,我从数据库加载的ID值存储为nvarchar(50),即使它是整数,我也尝试在SSIS中将其用作整数,并保持给我同样的错误信息,这对我有用:

SELECT CAST(id as INT) FROM dbo.Table