SQL 触发器检查之前的值
SQL trigger to check previous value
我在 table tblA
上有一个触发器,如果状态为 closed
,它会向 table 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
SQL 服务器中的触发器如果无法访问 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';
我在 table tblA
上有一个触发器,如果状态为 closed
,它会向 table 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
SQL 服务器中的触发器如果无法访问 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';