2
所以我創建了一個比較前後更新的觸發器,並確定where子句中指定的特定字段是否已更改。如果是這樣,我將先前信息的快照插入到歷史記錄表中。比較觸發器中的字段
問題是當字段創建時使用空值(給定業務規則,它合法爲空),並且在進行更新時,無法評估字符串不等於空值。我想捕獲它是空的發送給我們,後來填寫。
我應該使用什麼方法來比較空字段而不影響性能?
這是SQL Server 2005
CREATE TRIGGER [prj].[TRG_Master_Projection_Upd_History] ON [prj].[Master_Projections]
AFTER UPDATE
AS
SET NOCOUNT ON -- Prevents the error that gets thrown after inserting multiple records
-- if multiple records are being updated
BEGIN
INSERT INTO prj.History_Projections (ProjectionID, Cancelled, Appeal, Description, Response_PatternID,
Proj_Mail_date, [3602_Mail_Date], Proj_Qty, [3602_Qty], Proj_Resp_Rate,
Bounce_Back, Nickels, Kits, Oversized_RE, ChangeComments,
Modification_Process, Modification_Date, Modification_User)
SELECT D.ProjectionID, D.Cancelled, D.Appeal, D.Description, D.Response_PatternID, D.Proj_Mail_Date,
D.[3602_Mail_Date], D.Proj_Qty, D.[3602_Qty], D.Proj_Resp_Rate, D.Bounce_Back, D.Nickels, D.Kits,
D.Oversized_RE, D.ChangeComments, D.Modification_Process, D.Modification_Date, D.Modification_User
FROM deleted as D
JOIN inserted as I
ON D.ProjectionID = I.ProjectionID
WHERE (I.Cancelled <> D.Cancelled
OR I.Appeal <> D.Appeal
OR I.Description <> D.Description
OR I.Response_PatternID <> D.Response_PatternID
OR I.Proj_Mail_Date <> D.Proj_Mail_Date
OR I.[3602_Mail_Date] <> D.[3602_Mail_Date]
OR I.Proj_Qty <> D.Proj_Qty
OR I.[3602_Qty] <> D.[3602_Qty]
OR I.Proj_Resp_Rate <> D.Proj_Resp_Rate
OR I.Bounce_Back <> D.Bounce_Back
OR I.Nickels <> D.Nickels
OR I.Kits <> D.Kits
OR I.Oversized_RE <> D.Oversized_RE)
END;
SET NOCOUNT OFF;
就像一個魅力微妙的數據類型的問題。不知道是否合併是一種選擇,但這似乎排除了。謝謝! – Mohgeroth 2010-10-22 18:38:06
還有,你的isnull的,確保第二個值是不會出現的東西。例如,你的身份都從1開始,然後在你的isnulls中你可以放一些像-9999的東西。這樣,如果記錄更新爲零,您可以獲得正確的邏輯。 – DForck42 2010-10-22 19:28:22