0

How can I return a new query if my first query didn't return anything? I'm using ssis to execute a stored procedure, if the stored procedure didn't return anything it should pass a new query that will be then saved to a new ole db destination.

Sample Query:

Declare @DepartureDate DATETIME = '4/16/2013',

begin 

select PassengerNumber,FromTime,ToTime,Remarks from table a where DepartureDate = @DepartureDate



if (@@ROWCOUNT = 0)
    begin


      Select
        '-' [PassengerNumber],
        '00:00' [FromTime],
        '00:00' [ToTime],
        'No Mismatch' [Remarks]
    end
End

My problem is that my ole db source returns the blank query instead of the new query provided by the if(@@rowcount = 0). So no data is transferred to the ole db source.

anonymous1110
  • 885
  • 4
  • 14
  • 28

2 Answers2

1

how about using UNION?

SELECT  PassengerNumber, FromTime, ToTime, Remarks 
FROM    tableA 
WHERE   DepartureDate = @DepartureDate
UNION   ALL
SELECT  '-' [PassengerNumber],
        '00:00' [FromTime],
        '00:00' [ToTime],
        'No Mismatch' [Remarks]
WHERE   0 = (SELECT COUNT(*) FROM tableA WHERE DepartureDate = @DepartureDate)
John Woo
  • 258,903
  • 69
  • 498
  • 492
1
DECLARE @count INT = -1;
SELECT @count = COUNT(*)FROM TABLE a;

IF (@count > 0)
BEGIN
    SELECT PassengerNumber,FromTime,ToTime,Remarks FROM TABLE a;
END
ELSE
    SELECT
        '-' [PassengerNumber],
        '00:00' [FromTime],
        '00:00' [ToTime],
        'No Mismatch' [Remarks];
    END
END
Anoop Verma
  • 1,495
  • 14
  • 19