我在我的SSIS包中使用Merge Statement。问题是,当我运行包时,它不会更新datetime列。它正确插入日期时间,但如果源数据库中有新的日期时间,则不会将它们从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
答案 0 :(得分: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基本上是未知数,因此无法将它们评估为“等于”或“不等于”。