我在表tblA
上有一个触发器,如果状态为closed
,它会向表tblB
插入一条记录。下面的代码工作正常,但我希望它只在以前的状态不是closed
时执行。我怎么检查呢?
ON tblA
AFTER UPDATE
AS
BEGIN
SET NOCOUNT ON
INSERT INTO tblB
(a_id, action, usr)
select a.ID, 'closed', a.usr
from tblA a
WHERE a.Status LIKE 'closed';
END
答案 0 :(得分:4)
如果SQL Server中的触发器无法访问inserted
或deleted
(或两者),则始终会怀疑它,因为这些是伪表,可以为您提供的详细信息改变
你想要这样的东西:
INSERT INTO tblB
(a_id, action, usr)
select a.ID, 'closed', a.usr
from inserted a
inner join
deleted d
on a.ID = d.ID
WHERE a.Status = 'closed'
AND d.Status != 'closed';