datetimessismerge-statement

SSIS Merge Statment Datetime not updated


I am using Merge Statement in my SSIS package. The problem is that it doesn't update the datetime column when i run the package. It inserts the datetime correctly but doesn't update them from NULL to some datetime if a new datetime is available in source database. Both source and destination has same column type (datetime(2),null). I am using the code below in SQL Task after truncating staging table.

MERGE abc.dbo.invoices AS targe

USING (SELECT 

   ID      
  ,cash_received_date
  ,schedule_datetime
  ,delivery_date

FROM Staging.dbo.tmpabcinvoices) AS sourc
ON targe.id = sourc.id
WHEN MATCHED and  
targe.schedule_datetime <> sourc.schedule_datetime
or
targe.delivery_date <> sourc.delivery_date
or
targe.cash_received_date <> sourc.cash_received_date

THEN UPDATE SET 
,targe.schedule_datetime=sourc.schedule_datetime
,targe.delivery_date=sourc.delivery_date
,targe.cash_received_date=sourc.cash_received_date

WHEN NOT MATCHED THEN
INSERT(

      old_invoiceid         
      ,cash_received_date
      ,schedule_datetime
      ,delivery_date

)
VALUES
(

      sourc.old_invoiceid
      ,sourc.cash_received_date
      ,sourc.schedule_datetime
      ,sourc.delivery_date

);
GO

Solution

  • You have a comma which shouldn't be there at the start of this line:

    ,targe.schedule_datetime=sourc.schedule_datetime
    

    Also, you'll need to add this to take care of the NULLs:

    targe.schedule_datetime <> sourc.schedule_datetime
    or (targe.schedule_datetime IS NULL AND sourc.schedule_datetime IS NOT NULL)
    or targe.delivery_date <> sourc.delivery_date
    or (targe.delivery_date IS NULL AND sourc.delivery_date IS NOT NULL)
    or targe.cash_received_date <> sourc.cash_received_date
    or (targe.cash_received_date IS NULL AND sourc.cash_received_date IS NOT NULL)
    

    While ANSI_NULLS is set to ON, NULLs are basically unknowns, so they can't be evaluated to either 'equal to' or 'not equal to'.