我有这个触发器
ALTER TRIGGER [dbo].[tInsertTaskFromOpportunityReassignment]
ON [dbo].[OpportunityBase]
FOR UPDATE
AS
BEGIN
IF UPDATE(owninguser)
BEGIN
-- do the task
END
END
如果owninguser实际发生了变化,我只想完成任务。我该如何确定?
由于
答案 0 :(得分:0)
ALTER TRIGGER [dbo].[tInsertTaskFromOpportunityReassignment]
ON [dbo].[OpportunityBase]
FOR UPDATE
AS
BEGIN
DECLARE HasChanged int = 0;
SELECT @HasChanged = 1
FROM Inserted AS I
INNER JOIN Deleted AS D
ON I.PK = D.PK
AND IsNull(I.owninguser,'~') <> IsNull(D.owninguser,'~')
IF @HasChanged = 1
BEGIN
-- do the task
END
END
比较Inserted和Deleted之间的字段值,并连接主键上的两个表。
答案 1 :(得分:0)
ALTER TRIGGER [dbo].[tInsertTaskFromOpportunityReassignment]
ON [dbo].[OpportunityBase]
FOR UPDATE
AS
BEGIN
/*
I will assume that your [dbo].[OpportunityBase] table has a PRIMARY KEY
or UNIQUE column that is immutable to join the inserted and deleted
tables. In this example, [OpportunityBaseId] is that column.
The SELECT query returns a set of all records that had the value of [owninguser]
changed. What you would do from that point is up to you.
*/
SELECT
i.[OpportunityBaseId], i.owninguser New_owninguser, d.owninguser Old_owninguser
FROM inserted i
JOIN deleted d
ON i.[OpportunityBaseId] = d.[OpportunityBaseId]
AND
(
--owninguser value was changed.
i.[owninguser] <> d.[owninguser] OR
--owninguser changed from non-NULL to NULL.
(i.[owninguser] IS NULL AND d.[owninguser] IS NOT NULL) OR
--owninguser changed from NULL to non-NULL.
(i.[owninguser] IS NOT NULL AND d.[owninguser] IS NULL)
)
END
GO
答案 2 :(得分:0)
我喜欢使用concat,尽管它可能会导致您出现NULL和''的问题:
CONCAT('', inserted.owninguser) <> CONCAT('', deleted.owninguser)