所以我得到了这个看起来像这个
的触发器CREATE TRIGGER trLoadingOvertime
ON trailerScheme
FOR UPDATE AS
IF (SELECT COUNT(*) FROM trailerScheme
WHERE DATEDIFF(mi, trailerScheme.expectedFinishTime, trailerScheme.finishTime) > 15) > 0
BEGIN
INSERT INTO errorTable2
SELECT trailerSchemeID FROM inserted
END
在BEGIN和END之间我需要从刚刚更新的trailerScheme中将值插入errorTable2。
我希望有人可以帮助我
更新: 当我使用这段代码时,它只给我NULL
DECLARE @id INT
SELECT
@id = deleted.trailerSchemeID
FROM
inserted
INNER JOIN
deleted
ON inserted.trailerSchemeID = deleted.trailerSchemeID
INSERT INTO errorTable2 VALUES(@id)
答案 0 :(得分:0)
你在找这个吗?
CREATE TRIGGER trLoadingOvertime
ON trailerScheme
FOR UPDATE AS
INSERT INTO errorTable2
SELECT trailerSchemeID
FROM inserted
WHERE DATEDIFF(mi, expectedFinishTime, finishTime) > 15
这是 SQLFiddle 演示。
答案 1 :(得分:0)
想出我的自我。这是正确的代码:
DROP TRIGGER trLoadingOvertime
GO
CREATE TRIGGER trLoadingOvertime
ON trailerScheme
FOR INSERT AS
IF EXISTS (SELECT expectedFinishTime, finishTime
FROM inserted
WHERE DATEDIFF(mi, expectedFinishTime, finishTime) > 15)
BEGIN
DECLARE @id INT
DECLARE @exFTime DATETIME
DECLARE @fTime DATETIME
DECLARE @delay INT
SET @id = (SELECT trailerSchemeID FROM inserted)
SET @exFTime = (SELECT expectedFinishTime FROM inserted)
SET @fTime = (SELECT finishTime FROM inserted)
SET @delay = (SELECT DATEDIFF(mi, @exFTime, @fTime))
INSERT INTO errorTable VALUES(@id, @delay)
END
我试图插入,但问题是这个触发器是由更新调用的,因此插入的是空的。