nunopacheco nunopacheco - 2 months ago 27
SQL Question

SSIS Lookup Transformation - SQL query not working

I have an SSIS package for deployment into a SQL Server 2012 SSISDB and it uses a Lookup transformation. I am using a result from a SQL query to perform the lookup comparison.

enter image description here

This does not work and I get all rows as "No matched".

The query is the following:

DECLARE @LastJobDate DATETIME

SELECT @LastJobDate = COALESCE(MIN(S.LastImportDate), DATEADD(DAY, -2, GETDATE()))
FROM Stations S INNER JOIN
Lines L ON S.ID_Line = L.ID_Line
WHERE L.Name LIKE 'lineType%' AND S.ImportData = 1 AND S.Active = 1

SELECT J.ID_Line, J.ID_Job, J.SerialNumber
FROM [Jobs] J INNER JOIN
[Lines] L ON J.ID_Line = L.ID_Line
WHERE L.Name LIKE 'lineType%'AND J.TimeStamp >= DATEADD(HOUR, -1, @LastJobDate)


By accident, I found that if I place a
[SET NOCOUNT ON]
at the beggining of the query, it will work.

DECLARE @LastJobDate DATETIME
SET NOCOUNT ON

SELECT @LastJobDate = COALESCE(MIN(S.LastImportDate), DATEADD(DAY, -2, GETDATE()))
FROM Stations S INNER JOIN
Lines L ON S.ID_Line = L.ID_Line
WHERE L.Name LIKE 'lineType%' AND S.ImportData = 1 AND S.Active = 1

SELECT J.ID_Line, J.ID_Job, J.SerialNumber
FROM [Jobs] J INNER JOIN
[Lines] L ON J.ID_Line = L.ID_Line
WHERE L.Name LIKE 'lineType%'AND J.TimeStamp >= DATEADD(HOUR, -1, @LastJobDate)


Am I missing something? Why this behavior?

Answer

Why this behavior?

An SSIS Lookup Component can only consider the first result returned by a multi-statement query such as yours.

When you don't have SET NOCOUNT ON, the first result returned by your query will be the message "1 row(s) affected" or something like that. The Lookup Component will not be able to look at the result set returned by the second half of your query.

This is why setting NOCOUNT ON fixes the problem. The "row(s) affected" message will not be returned by the first part of the query, and the only thing returned will be the resultset of the second part of the query, which the Lookup Component will then process.

Comments