由於太難解釋的原因,我在表中有三列包含日期和時間值。一列僅保存日期,第二列僅保存時間,第三列保存值DATETIME
。看起來像這樣:當從NULL更新值時,SQL Server AFTER UPDATE觸發器不工作
OPPORTUNITYID | ... | ProductionDate | ProductionTime | PRODUCTIONDATETIME
-------------------------------------------------------------------------------
091798-324971 | ... | 12-07-2014 | 11:30 AM | 2014-07-12 11:30:00:000
然後我有一個觸發器,保持這些值同步,無論哪個正在更新。 這是觸發(部分):
CREATE TRIGGER [dbo].[TBL_OPPORTUNITY_DUEDATES_TRU]
ON [dbo].[TBL_OPPORTUNITY]
AFTER UPDATE
AS
BEGIN
SET NOCOUNT ON;
IF UPDATE (PRODUCTIONDATETIME)
BEGIN
UPDATE TBL_OPPORTUNITY
SET ProductionDate = CONVERT(VARCHAR(10), PRODUCTIONDATETIME, 105)
, ProductionTime = REPLACE(REPLACE(RIGHT('0'+LTRIM(RIGHT(CONVERT(VARCHAR, PRODUCTIONDATETIME,100), 7)), 7), 'AM', ' AM'), 'PM', ' PM')
WHERE OPPORTUNITYID IN (SELECT i.OPPORTUNITYID FROM inserted i
INNER JOIN deleted d ON i.OPPORTUNITYID = d.OPPORTUNITYID
WHERE NOT i.PRODUCTIONDATETIME = d.PRODUCTIONDATETIME
AND NOT (i.PRODUCTIONDATETIME = '' OR i.PRODUCTIONDATETIME IS NULL)
);
END
IF (UPDATE (ProductionDate) OR UPDATE (ProductionTime))
BEGIN
UPDATE TBL_OPPORTUNITY
SET PRODUCTIONDATETIME = CONVERT(DATETIME, ProductionDate + ' ' + ProductionTime, 105)
WHERE OPPORTUNITYID IN (SELECT i.OPPORTUNITYID FROM inserted i
INNER JOIN deleted d ON i.OPPORTUNITYID = d.OPPORTUNITYID
WHERE (NOT i.ProductionDate = d.ProductionDate
OR NOT i.ProductionTime = d.ProductionTime)
AND NOT (i.ProductionDate = '' OR i.ProductionDate IS NULL)
AND NOT (i.ProductionTime = '' OR i.ProductionTime IS NULL)
);
UPDATE TBL_OPPORTUNITY
SET PRODUCTIONDATETIME = CONVERT(DATETIME, ProductionDate + ' 12:00:00', 105)
WHERE OPPORTUNITYID IN (SELECT i.OPPORTUNITYID FROM inserted i
INNER JOIN deleted d ON i.OPPORTUNITYID = d.OPPORTUNITYID
WHERE (NOT i.ProductionDate = d.ProductionDate
OR NOT i.ProductionTime = d.ProductionTime)
AND NOT (i.ProductionDate = '' OR i.ProductionDate IS NULL)
AND (i.ProductionTime = '' OR i.ProductionTime IS NULL)
);
END
END
GO
觸發按預期工作時的任何值被更新。但是,如果值正在從NULL
更新,或者換句話說,列中的舊值爲NULL
,並且新值爲(例如'02 -03 -12'),則觸發器將失敗(因爲不會進行任何更改) 2014' 。
這是爲什麼?
服務器是Microsoft SQL Server 2008 R2。
謝謝你的任何線索。
如果更新將所有3列更改爲不一致的值,您打算如何處理?哪些值是「正確的」? –
這絕不會發生,因爲值是從不同的來源改變的 - 一個來源只改變「組分」值,另一個來源只改變「合成」值。 –