SQL Server触发器 - 适用于UPDATE但不适用于INSERT

时间:2017-03-15 20:06:52

标签: sql-server

以下触发器(更新与插入或更新相同的表中的列)完全适用于UPDATE,但似乎没有触发INSERT。是否可以在?

上运行更新

我错过了什么?

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER TRIGGER [dbo].[OnEvaluationCreateOrEdit] 
ON [dbo].[tblEvaluationFormResultsSummary] 
AFTER INSERT, UPDATE
AS 
BEGIN
    SET NOCOUNT ON;

    -- get the last id value of the record inserted or updated
    DECLARE @id  BIGINT;

    SELECT @id = EvaluationFormResultSummaryID
    FROM INSERTED;

    -- Insert statements for trigger here
    UPDATE A
    SET A.Outcome =  CASE 
                        WHEN B.[# of fail alls] > 0 THEN 'Poor' 
                        WHEN B.[# of Fail Amber] / [# of Live Questions] >= 0.3 THEN 'Poor'
                        WHEN B.[# of Fail Amber] > 0 THEN 'Development Needed'
                        WHEN B.[# of Fail Green] / [# of Live Questions] >= 0.4 THEN 'Development Needed'
                        WHEN B.[# of Fail Green] > 0 THEN 'Right First Time - Coaching Required'
                        ELSE 'Right First Time'
                     END
    FROM
        dbo.tblEvaluationFormResultsSummary AS A
    INNER JOIN 
        (SELECT        
             SUM(CASE WHEN FormQuestionRatingOptionFailAll = 1 THEN 1 ELSE 0 END) as [# of fail alls],
             SUM(CASE WHEN FormQuestionRatingOptionIsNA = 1 THEN 1 ELSE 0 END) as [# of N/A],
             SUM(CASE WHEN FormQuestionRatingOptionLabel = 'Fail / Amber' THEN 1 ELSE 0 END) as [# of Fail Amber],
             COUNT(EFRS.EvaluationFormResultsSummaryID) as [No of Questions],
             COUNT(EFRS.EvaluationFormResultsSummaryID) - SUM(CASE WHEN FormQuestionRatingOptionIsNA = 1 THEN 1 ELSE 0 END) as [# of Live Questions],
             SUM(CASE WHEN FormQuestionRatingOptionLabel = 'Fail / Green' THEN 1 ELSE 0 END) AS [# of Fail Green],
             EFRS.EvaluationFormResultsSummaryID
         FROM  
             dbo.tblEvaluationFormResultsDetails AS EFRS 
         INNER JOIN 
             dbo.tblEvaluationFormQuestionRatingOptions AS RO ON EFRS.QuestionRatingOptionID = RO.FormQuestionRatingOptionID
         WHERE        
             (EFRS.EvaluationFormResultsSummaryID = @id)
         GROUP BY 
             EFRS.EvaluationFormResultsSummaryID) B ON a.EvaluationFormResultSummaryID = B.EvaluationFormResultsSummaryID
    WHERE 
        A.EvaluationFormResultSummaryID = @id
END

提前致谢!

0 个答案:

没有答案