1

我在我的 SSIS 包中使用合并语句。问题是当我运行包时它不会更新日期时间列。如果源数据库中有新的日期时间可用,它会正确插入日期时间,但不会将它们从 NULL 更新到某个日期时间。源和目标都具有相同的列类型 (datetime(2),null)。截断临时表后,我在 SQL 任务中使用下面的代码。

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
4

1 回答 1

2

你有一个逗号,它不应该出现在这一行的开头:

,targe.schedule_datetime=sourc.schedule_datetime

此外,您需要添加它来处理 NULL:

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)

虽然 ANSI_NULLS 设置为 ON,但 NULL 基本上是未知数,因此不能将它们评估为“等于”或“不等于”。

于 2017-09-18T09:05:28.733 回答